Reputation: 5012
Hello I have try to add List in my model and add data to my homepage but I have this error only when I add " suite=widget.todo.suite;
in the initstate :
suite=widget.todo.suite; => type 'List<dynamic>' is not a subtype of type 'List<String>'
If I use an other data model as "id" who is a string or "isDone" who is a bool I have no error. But my "suite" data have error
I don't understand.
-------------------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,
};
}
Edit: if I change list suite par list suite
my Streambuilder return error. If I use list I have no error on streambuilder but an other error with dynamic type
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();
}
}
},
),
Upvotes: 0
Views: 186
Reputation: 2461
In Todo
class, you need to specify your List
type :
List<String> suite;
By default, if you not specify type, it's dynamic
type :
List suite;
is egal to List<dynamic> suite;
If you can't specify a type in Todo
class, you can cast your dynamic List
when you set it in suite
:
suite = widget.todo.suite as List<String>;
You also have to convert your data in fromJson
function. Try this :
suite: List<String>.from(json['suite']),
Upvotes: 1