【问题标题】:Flutter: Unable to read data from firebase. The method '[]' was called on null errorFlutter:无法从 firebase 读取数据。方法 '[]' 在 null 错误上被调用
【发布时间】:2020-07-31 17:03:31
【问题描述】:

我在扫描条形码后尝试从 firebase 读取数据。

它应该是这样显示的,但它应该显示数据库中的名称和价格,而不是条形码 (https://m.imgur.com/gallery/lEFJZ0Q)

代码:

class ListTileModel {
  String barcode;
  ListTileModel(this.barcode);
}

以下代码在有状态小部件内

List<ListTileModel> _items = [];
String barcode = "";

  void _add() {
    _items.add(ListTileModel(barcode));
      setState(() {});
  }


  @override
  void initState(){
    super.initState();
  }

使用的StreamBuiler:

new Container(

              child: ListView(
                  children: _items
                      .map((item) => StreamBuilder(
                  stream: FirebaseDatabase.instance.reference().child('prd').child(item.barcode).onValue,
                  builder: (context, snap) {
                    print(item.barcode);
                    if(snap.hasData){
                      DataSnapshot snapshot = snap.data.snapshot;
                      Map<dynamic, dynamic> itm = snapshot.value;
                      return snap.data.snapshot.value == null
                          ? SizedBox()
                          : ListView.builder(
                        shrinkWrap: true,
                        scrollDirection: Axis.vertical,
                        itemCount: 1,
                        itemBuilder: (context, index) {
                          return Row(
                            children: <Widget>[

                              ConstrainedBox(

                                child: Container(

                                  child: Text(itm[index]['name'],style: TextStyle(fontSize: 20),),
                                ),
                              ),
                              SizedBox(width: 100,),
                              ConstrainedBox(

                                child: Container(

                                  child: Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),
                                ),
                              ),
                            ],
                          );
                        },
                      );
                    }
                    else {
                      return   Center(child: CircularProgressIndicator());
                    }
                  }
              ),
                  ).toList()
              ),
            ),

条码扫描码:

小部件:

floatingActionButton: FloatingActionButton(
          onPressed: scan,
        child: Icon(Icons.add_shopping_cart),

      ),

扫描():

Future scan() async{
    try{
      String barcode = await BarcodeScanner.scan();
        setState(() {
          this.barcode = barcode;
        });
      _add();
    }on PlatformException catch(e) {
      if(e.code == BarcodeScanner.CameraAccessDenied){
        setState(() {
          this.barcode = 'Access Denied';
        });
      }
    }

我收到以下错误:

以下 NoSuchMethodError 被抛出构建: 在 null 上调用了方法“[]”。 接收方:空 尝试调用:

【问题讨论】:

  • 我认为您没有获得价值,因为他们的 det 也在那里。尝试以下查询。流:FirebaseDatabase.instance.reference().child('prd').child(item.barcode).child('det').onValue,
  • @VirenVVarasadiya 我试过了,但同样的错误
  • 参考@wcyankees424 的建议解决了这个问题。

标签: firebase flutter firebase-realtime-database stream-builder flutter-widget


【解决方案1】:

请试试这个,如果它解决了您需要更改的问题,请告诉我

Text(itm[index]['name'],style: TextStyle(fontSize: 20),),

Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),

Text(itm['det']['name'],style: TextStyle(fontSize: 20),),

Text(itm['det']['price'].toString(),style: TextStyle(fontSize: 20),),

让我知道这是否适合您。我相信问题也是索引。 现在你的说法。

【讨论】:

  • 我试过了,但仍然是同样的错误:在构建时抛出了以下 NoSuchMethodError:在 null 上调用了方法 '[]'。接收方:null 尝试调用:[]("det")
  • 是的,我试过了,我不认为将条形码作为索引传递。我得到了同样的错误
  • 很抱歉,我没有看到它在您的信息流中的部分
  • 我还有一个。
  • 尝试将索引全部取出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-07
  • 2021-12-18
  • 2021-02-16
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多