Flutter:在空脏状态下调用吸气剂“长度”

时间:2019-12-01 09:22:47

标签: flutter

我使用Future构建器从json文件中获取数据,但是在运行应用程序时出现错误消息,列表正常运行,但该消息仍然出现。我希望知道问题的解决方案 我在另一个文件中使用了将来的构建器,但是错误涉及到此类

错误消息是=>调用getter'length'为null。

错误消息=>

I/flutter (24488): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (24488): The following NoSuchMethodError was thrown building FutureBuilder<String>(dirty, state:
I/flutter (24488): _FutureBuilderState<String>#10ff6):
I/flutter (24488): The getter 'length' was called on null.
I/flutter (24488): Receiver: null
I/flutter (24488): Tried calling: length
I/flutter (24488): Widget creation tracking is currently disabled. Enabling it enables improved error messages. It can
I/flutter (24488): be enabled by passing `--track-widget-creation` to `flutter run` or `flutter test`.
I/flutter (24488): When the exception was thrown, this was the stack:
I/flutter (24488): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter (24488): #1      _parseJson (dart:convert-patch/convert_patch.dart:28:28)
I/flutter (24488): #2      JsonDecoder.convert (dart:convert/json.dart:493:36)
I/flutter (24488): #3      JsonCodec.decode (dart:convert/json.dart:151:41)
I/flutter (24488): #4      CateogryState.build.<anonymous closure> (package:e_menu/content/cateogry_list.dart:23:33)
I/flutter (24488): #5      _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
I/flutter (24488): #6      StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
I/flutter (24488): #7      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
I/flutter (24488): #8      Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
I/flutter (24488): #9      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3924:5)
I/flutter (24488): #10     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4088:11)
I/flutter (24488): #11     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3919:5)

代码=>

          child: FutureBuilder(
              future: recipeList.getMethod(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                List snap = snapshot.data;
                List temp = [];

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
                if (snapshot.hasError) {
                  return Center(
                    child: new Text('Error ${snapshot.error}'),
                  );
                }

                if (widget.currentcatelog == 'All') {
                  temp = snap;
                } else {
                  snap.asMap().forEach((index, value) {
                    if (snap[index]['catelog'] == widget.currentcatelog) {
                      temp.add(value);
                    }
                  });
                }
                return Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: (temp.length == 0)
                      ? Center(child: Text('No file for it'))
                      : GridView.builder(
                          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: MediaQuery.of(context).orientation ==
                                    Orientation.portrait
                                ? 2
                                : 3,
                            childAspectRatio: (1 / 1.2),
                          ),
                          physics: BouncingScrollPhysics(),
                          itemCount: temp.length,
                          scrollDirection: Axis.vertical,
                          itemBuilder: (BuildContext context, int index) {
                            if (! counter.containsKey(index)) {
                               counter[index] = 0;
                              print( counter[index]);
                            }
                            return Padding(
                              padding: const EdgeInsets.all(12),
                              child: GestureDetector(
                                onTap: () {
                                  showDialog(
                                      context: context,
                                      builder: (context) {
                                        return Item(list: snap,
                                        index: index);
                                      });
                                },
                                child: Hero(
                                  tag: '${snap[index]['catelog']}${widget.name}',
                                  child: SizedBox(
                                    width: 70,
                                    child: Card(
                                      color: Colors.transparent,
                                      elevation: 10.0,
                                      child: Row(
                                        children: <Widget>[
                                          Expanded(
                                            child: ClipRRect(
                                              borderRadius:
                                                  BorderRadius.circular(20.0),
                                              child: Stack(
                                                fit: StackFit.expand,
                                                children: <Widget>[
                                                  Container(
                                                    width: double.infinity,
                                                    child: Image.asset(
                                                      snap[index]['pic'],
                                                      fit: BoxFit.cover,
                                                    ),
                                                  ),
                                                  Align(
                                                      alignment:
                                                          Alignment.bottomCenter,
                                                      child: Container(
                                                        padding: EdgeInsets.symmetric(vertical: 10,horizontal: 30),
                                                        color: Colors.black38,
                                                        width:
                                                            MediaQuery.of(context)
                                                                .size
                                                                .width,
                                                        height: MediaQuery.of(context)
                                                                .size
                                                                .width/8.5,
                                                        child: Column(
                                                          mainAxisAlignment:
                                                              MainAxisAlignment
                                                                  .start,
                                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                          children: <Widget>[
                                                            Text(
                                                                snap[index]
                                                                    ['name'],
                                                                style: TextStyle(
                                                                    color: Colors
                                                                        .white,
                                                                    fontSize:
                                                                        18)),
                                                            Text(
                                                                '${snap[index]['price']} BHD',
                                                                style: TextStyle(
                                                                    color: Colors
                                                                        .white,
                                                                    fontSize:
                                                                        18)),
                                                          ],
                                                        ),
                                                      ))
                                                ],
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            );
                          },
                        ),
                );
              })),```

0 个答案:

没有答案