【问题标题】:flutter: operator '[]' isn't defined for the type 'JsonCodec'颤振:未为“JsonCodec”类型定义运算符“[]”
【发布时间】:2021-08-06 13:20:48
【问题描述】:

我从事颤振项目。我尝试使用 pagination 从服务器获取数据。我这里有问题:

myList = List.generate(10, (index) => DataObd.fromJson(json[index]));

错误:没有为类型“JsonCodec”定义运算符“[]”。

我的代码:

class _StatusState extends State<Status> {

  List<DataObd> myList;
  ScrollController _scrollController = ScrollController();
  int _currentMax = 10;
  ObdApi obdApi = ObdApi();
  @override
  void initState() {
    super.initState();
    myList = List.generate(10, (index) => DataObd.fromJson(json[index]));
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getMoreData();
      }
    });
  }

  _getMoreData() {

    print('hello');
   /* for (int i = _currentMax; i < _currentMax + 10; i++) {
      myList.add("Item : ${i + 1}");
    }
    _currentMax = _currentMax + 10;

    setState(() {});*/
  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        minimum: const EdgeInsets.all(10.0),
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
              backgroundColor: Color(0xFF010611),
              iconTheme: IconThemeData(color: Colors.white),
              automaticallyImplyLeading: true,
              centerTitle: true,
              title: Text(
                'Status',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              elevation: 0.0,
              leading: Row(
                children: [
                  IconButton(
                    icon: Icon(
                      CommunityMaterialIcons.arrow_left_circle_outline,
                      color: Colors.yellow[600],
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              )),
           body: Container(
              child: FutureBuilder<ActiveObd>(
                  future: obdApi.getActiveObd(),

                  builder: (context, snapshot) {

                    switch (snapshot.connectionState) {
                      case ConnectionState.none:
                        return Text('no connection');
                      case ConnectionState.active:
                      case ConnectionState.waiting:
                        return Center(
                          child: CircularProgressIndicator(),
                        );
                        break;
                      case ConnectionState.done:
                        if (snapshot.hasError) {
                          return Center(
                            child:
                            new CircularProgressIndicator(),
                          );
                        } else {
                          if (snapshot.hasData) {
                            var activeobd = snapshot.data;
                            return ListView.builder(
                                controller: _scrollController,
                                itemExtent: 80,
                              itemBuilder: (context , index){
                                if (index == myList.length) {
                                  return CupertinoActivityIndicator();
                                }
                                final obd = activeobd.obds[index];
                                DateTime date = DateTime.parse(obd.dateOBDCommand);
                                String result = DateFormat('yyyy-MM-dd H:m:s').format(date);
                                return Card(
                                  child: Column(
                                    mainAxisSize: MainAxisSize.min,
                                    children: <Widget>[
                                      ListTile(
                                        title: Container(
                                          height: 30,
                                            child: Text("${obd.description}")),
                                        subtitle: Text(result.toString()),
                                        trailing: Text("${obd.value}"),
                                      )
                              itemCount:myList.length +1,
    
                            );

                          } else {
                            return Text('No Data');
                          }
                        }
                        break;
                      default:
                        return Container();
                        break;
                    }

我该如何解决?

提前致谢

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    此错误 The operator '[]' is not defined for type Map Function() 告诉您您尝试对其执行 [] 运算符的对象是一个函数。在您的情况下, .data 实际上不是成员变量,而是一个函数。只需在 data 关键字旁边添加 () 即可解决,因此您的错误行(已修复)如下所示:

    document.data()['listCategories'] ?? [];

    这可能是因为您更新了 firebase 核心包。以前只是 .data[],但现在是 .data()[]。

    【讨论】:

    • 我没有使用 firebase 包。 @Gurjot
    猜你喜欢
    • 2020-03-05
    • 2020-12-28
    • 2021-11-30
    • 2021-06-11
    • 2021-09-15
    • 2021-01-03
    • 1970-01-01
    • 2020-10-10
    • 2020-12-09
    相关资源
    最近更新 更多