【问题标题】:Error: type 'List<dynamic>' is not a subtype of type 'List<DataRow>'错误:类型“List<dynamic>”不是“List<DataRow>”类型的子类型
【发布时间】:2021-11-10 05:27:15
【问题描述】:

我想以表格形式显示数据 我正在尝试在数据表格式中显示其余的 api 数据和 得到错误: .

在构建 FutureBuilder(dirty, state: _FutureBuilderState#828b1) 时引发了以下 _TypeError: “列表”类型不是“列表”类型的子类型 相关的导致错误的小部件是: FutureBuilder file:///G:/Flutter_Workspace/flutter_app_network_call/lib/main.dart:38:14 抛出异常时,这是堆栈: #0 _MyAppState.build.

(包:flutter_app_network_call/main.dart:68:14) #1 _FutureBuilderState.build (包:flutter/src/widgets/async.dart:775:55) #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) ...

══════════════════════════════════════════ p>

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
void main() => runApp(MaterialApp(
    home: MyApp(),
));

class MyApp extends StatefulWidget {

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  Future<List<Post>> getPost() async{
    var data = await http.get(Uri.parse("https://jsonplaceholder.typicode.com/posts"));
    var jsonData = json.decode(data.body);

    List<Post> posts = [];

    for (var _post in jsonData){
      Post post = Post(_post['id'], _post['userId'], _post['title'], _post['body']);
      posts.add(post);
    }

    print(posts.length);
    return posts;

  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPost(),
        builder: (BuildContext context, AsyncSnapshot snapshot){

          if(snapshot.data == null){
              return Container(
                child: Center(
                  child: Text('Loading...'),
                ),
              );
          }else{
            for(var pst in snapshot.data){
              print(pst.body);
            }
            return DataTable(
                columns: [
                  DataColumn(label: Text('Id')),
                  DataColumn(label: Text('Body'))
                ],
                rows: snapshot.data
                .map(
                (data) => DataRow(
                cells: [
                  DataCell(
                    Text(data.id.toString()),
                  ),
                  DataCell(
                    Text(data.body),
                  )
                ]),
          ) .toList(),
            );
          }
        },
      ),
    );
  }
}


class Post{
  final int id;
  final int userId;
  final String title;
  final String body;

  Post(this.id, this.userId, this.title, this.body);
}

【问题讨论】:

  • 参考我的回答here希望对您有所帮助

标签: flutter dart datatable


【解决方案1】:

指定map的类型为map&lt;DataRow&gt;

    return DataTable(
      columns: [DataColumn(label: Text('Id')), DataColumn(label: Text('Body'))],
      rows: snapshot.data
          .map<DataRow>(
            (data) => DataRow(cells: [
              DataCell(
                Text(data.id.toString()),
              ),
              DataCell(
                Text(data.body),
              )
            ]),
          )
          .toList(),
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2021-08-13
    • 2021-12-29
    • 2023-01-08
    • 2021-04-07
    • 2022-01-23
    • 2021-04-13
    • 2021-06-24
    相关资源
    最近更新 更多