【问题标题】:why my list model show this error : type 'List<dynamic>' is not a subtype of type 'List<String>'?为什么我的列表模型显示此错误:类型“List<dynamic>”不是“List<String>”类型的子类型?
【发布时间】: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


    【解决方案1】:

    Todo 类中,您需要指定您的List 类型:

    List&lt;String&gt; suite;


    默认情况下,如果不指定类型,则为dynamic类型:

    List suite; 等于 List&lt;dynamic&gt; suite;


    如果您无法在Todo 类中指定类型,则可以在suite 中设置动态List 时进行转换:

    suite = widget.todo.suite as List&lt;String&gt;;

    编辑

    您还必须在fromJson 函数中转换您的数据。试试这个:

    suite: List&lt;String&gt;.from(json['suite']),

    【讨论】:

    • 谢谢我将 suite = widget.todo.suite 更改为 List 但我的类型 'List' 不是类型转换中类型 'List' 的子类型。我也改变了 List 套件;但是我的流构建器返回错误,我使用流构建器编辑我的帖子
    • widget.todo.suite 中有哪些数据?字符串列表?
    • 是的,这是我发送到 firebase 并尝试读取/转换为显示的字符串列表
    • 您还需要更新您的fromJson 函数。我更新了我的答案。
    猜你喜欢
    • 2022-12-02
    • 2021-12-29
    • 2023-01-08
    • 2019-06-26
    • 2021-07-06
    • 2021-02-17
    • 2021-03-17
    • 2021-04-07
    • 2021-04-13
    相关资源
    最近更新 更多