【发布时间】:2020-11-08 22:14:34
【问题描述】:
我正在尝试在我的应用程序中解析嵌套的 JSON 文档。 JSON 结构如下所示:
[
{
"id": 1,
"content": [
{
"type": "text",
"value": "This is a Text1"
},
{
"type": "latex",
"value": "\frac00"
},
{
"type": "text",
"value": "This is a Text2"
},
{
"type": "latex",
"value": "\frac00"
},
{
"type": "text",
"value": "This is a Text3"
}
]
},
{
"id": 2,
"content": [
{
"type": "text",
"value": "This is a Text"
}
]
}
] 这是我的模型类:
class Tutorial {
String id;
List<Content> content;
Tutorial({this.id, this.content});
Tutorial.fromJson(Map<String, dynamic> json) {
id = json['id'];
if (json['content'] != null) {
content = new List<Content>();
json['content'].forEach((v) {
content.add(new Content.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
if (this.content != null) {
data['content'] = this.content.map((v) => v.toJson()).toList();
}
return data;
}
}
class Content {
String type;
String value;
Content({this.type, this.value});
Content.fromJson(Map<String, dynamic> json) {
type = json['type'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
data['value'] = this.value;
return data;
}
}
这就是我检索该 Json 并制作响应对象的方式:
import 'package:Mathzi/pages/courses/models/tutorialModel.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'dart:async' show Future;
import 'dart:convert' as convert;
class TutorialService {
Future<List> fetchTutorial() async {
var response = await rootBundle.loadString('assets/tutorial.json');
final jsonResponse = convert.jsonDecode(response) as List;
return jsonResponse.map((tutorial) => Tutorial.fromJson(tutorial)).toList();
}
}
这是我的屏幕小部件树:
final TutorialService tutorialService = TutorialService();
@override
Widget build(BuildContext context) {
return FutureProvider(
create: (context) => tutorialService.fetchTutorial(),
catchError: (context, error) => print(error.toString()),
child: SizeTransition(
axis: Axis.vertical,
sizeFactor: animation,
child: GestureDetector(
//behavior: HitTestBehavior.opaque,
onTap: onTap,
child: SizedBox(
height: 50.0,
width: MediaQuery.of(context).size.width,
child: TutParagraph()
),
),
),
);
}
还有我的 TutParagraph.dart:
import 'package:Mathzi/pages/courses/models/tutorialModel.dart';
import 'package:catex/catex.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'models/tutorialModel.dart';
class TutParagraph extends StatelessWidget {
const TutParagraph({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Content> parag = Provider.of<List<Content>>(context);
return (parag == null)
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: parag.length,
itemBuilder: (context, index) {
if (parag[index].type.toString() == "text")
return Text(parag[index].value.toString());
else if (parag[index].type.toString() == "latex")
return CaTeX(parag[index].value.toString());
else
return null;
},
);
}
}
如果类型等于 text 我使用 Text() 小部件来显示它,如果是 latex 我使用 CaTex()
当我运行我的代码时,它给了我这个错误信息:
错误:
在此上方找不到正确的 Provider
TutParagraph 小部件
要修复,请:
- 确保 Provider
是它的祖先 TutParagraph Widget * 向 Provider
提供类型 * 提供类型给消费者
* 提供类型给 Provider.of
() * 确保正确的
context正在 用过。
【问题讨论】:
-
尝试将类型赋予
FutureProvider<List>,我也没有看到<List<Content>>类型的提供者 -
@EdwynZN 我的 json 响应已经是一个列表。这里:return jsonResponse.map((tutorial) => Tutorial.fromJson(tutorial)).toList();这是我的 Content
- ,它是 Tutorial
- List
parag = Provider.of - >(context);
-
是的,我看到了,但是提供者只看到一个列表,不知道该列表的每个元素内都有一个嵌套的 Content 类型,所以你不能这样称呼它,@987654331 @ 不会给你任何错误,因为这是你从未来返回的正确类型的 List
-
现在它给出了这个错误:错误:在这个 TutParagraph Widget 上方找不到正确的 Provider
- >
标签: flutter dart flutter-provider