【问题标题】:Unhandled Exception: NoSuchMethodError: The method '[]' was called on null on flutter未处理的异常:NoSuchMethodError:方法“[]”在颤振上被调用为 null
【发布时间】:2021-08-06 12:34:32
【问题描述】:

我想在列表视图中显示数据,如下所示(那个是硬编码的)

当我将其更改为列表时,出现此错误:

E/flutter ( 6925): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 6925): Receiver: null
E/flutter ( 6925): Tried calling: []("interest")

这是我的小部件构建:

@override
    Widget build(BuildContext context) {
      timeDilation = 2.0;
      return WillPopScope(
        onWillPop: () async => false,
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: kYellow,
          appBar: AppBar(
            leading: Hero(
              tag: 'animasilogo',
              child: Container(
                margin: EdgeInsets.all(5.0),
                child: Image.asset('assets/img/lemonlime.png'),
              ),
            ),
            title: Text('Simulator'),
            actions: [
              IconButton(
                icon: const Icon(Icons.exit_to_app),
                onPressed: () {
                  SharedPref.signOut();
                  Navigator.pushNamed(context, LoginScreen.id);
                },
              )
            ],
          ),
          body: RefreshIndicator(
            onRefresh: () => _fetchData(),
            child: loading
                ? Center(child: CircularProgressIndicator())
                : ListView.builder(
              itemCount: _loanmodel.length,
              itemBuilder: (context, i) {
              final data =_loanmodel[i];
              return Container(
                alignment: Alignment.topLeft,
                margin: const EdgeInsets.only(top: 15.0),
                  padding: EdgeInsets.all(20.0),
                child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                 Text("Interest :  " + data.interest , style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 1.5), textAlign: TextAlign.justify),
                  Text("Balance :  " + data.balance,  style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 1.5),textAlign: TextAlign.justify),
                  Text("Principal : " + data.principal,  style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 1.5),textAlign: TextAlign.justify),
                  Text("Installment :" + data.installment,  style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 1.5),textAlign: TextAlign.justify),
                  Divider(
                    color: Colors.black,
                  ),

                ],
              ),
                );
              },
            ),
          ),
        ),
      );
    }
  }

API、Model等功能可以查看我在Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' flutter的另一篇文章

【问题讨论】:

  • 数据好像是空的。所以你应该验证data 不为空。
  • 数据来自value periodtime、interestpermonth和loanamountrequest。输入该值时,将显示 [{"No":0,"interest":"0.00","balance":"15,000,000.00","principal":"0.00","Installment":"0.00","状态":true},

标签: flutter


【解决方案1】:

确保您收到的部分数据不为空。 应该收到“兴趣”的部分为空,因为它是您尝试使用的数据的第一部分,其他部分也可能为空

【讨论】:

  • 另一个是Unhandled Exception: type '(dynamic) => LoanModel' is not a subtype of type '(String, dynamic) => MapEntry' of 'transform'跨度>
  • 这是因为您没有将 json 映射更改为模型
  • 输出显示,但它说错误我的 API 没有返回为列表 [{"No":0,"interest":"0.00","balance":"1,200,000.00","principal": "0.00","分期付款":"0.00","状态":true},{"否":1,"利息":"12,000.00","余额":"0.00","本金":"1,200,000.00", "Installment":"1,212,000.00","Status":true}] I/flutter (8054): true E/flutter (8054): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:类型'_InternalLinkedHashMap' 不是类型 'Iterable' 的子类型
猜你喜欢
  • 2020-04-25
  • 2022-11-17
  • 1970-01-01
  • 2021-06-08
  • 2020-09-28
  • 2019-07-25
  • 1970-01-01
  • 2020-12-09
  • 2020-12-26
相关资源
最近更新 更多