【发布时间】:2021-09-05 20:02:18
【问题描述】:
您好,我尝试在我的模型中添加 List 并将数据添加到我的主页,但只有当我在 initstate 中添加 " suite=widget.todo.suite; 时才会出现此错误:
suite=widget.todo.suite; => type 'List<dynamic>' is not a subtype of type 'List<String>'
如果我使用其他数据模型作为字符串的“id”或作为布尔值的“isDone”,我没有错误。但我的“套件”数据有错误
我不明白。
-------------------homepage--------------
class Add_suite extends StatefulWidget {
final Todo todo;
const Add_suite({Key key, @required this.todo}) : super(key: key);
@override
_Add_suiteState createState() => _Add_suiteState();
}
class _Add_suiteState extends State<Add_suite> {
final _formKey = GlobalKey<FormState>();
String title;
String description;
List<String> suite =[""];
List<String> stringList = [];
@override
void initState() {
super.initState();
Firebase.initializeApp().whenComplete(() {
print("completed");
setState(() {});
});
suite=widget.todo.suite;
title = widget.todo.title;
description = widget.todo.description;
}
...
}
-------------------model--------------
class Todo {
DateTime date;
String title;
String id;
String description;
List suite;
bool isDone;
Todo({
@required this.date,
@required this.title,
this.description = '',
this.suite,
this.id,
this.isDone = false,
});
static Todo fromJson(Map<String, dynamic> json) => Todo(
date: Utils.toDateTime(json['createdTime']),
title: json['title'],
description: json['description'],
suite: json['suite'],
id: json['id'],
isDone: json['isDone'],
);
Map<String, dynamic> toJson() => {
'date': Utils.fromDateTimeToJson(date),
'title': title,
'description': description,
'suite': suite,
'id': id,
'isDone': isDone,
};
}
编辑:如果我更改 list suite par list suite
我的 Streambuilder 返回错误。如果我使用列表,我在 streambuilder 上没有错误,但是动态类型的另一个错误
StreamBuilder<List<Todo>>(
stream: FirebaseFirestore.instance
.collection('first_stories')
.orderBy("date", descending: true)
.snapshots()
.transform(Utils.transformer(Todo.fromJson)),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.hasError) {
return buildText('Erreur');
} else {
final todos = snapshot.data;
final provider = Provider.of<TodosProvider>(context);
provider.setTodos(todos);
return
TodoListWidget();
}
}
},
),
【问题讨论】:
标签: flutter