Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api 无法在itemCount中获取数据的长度_Api_Flutter_Asynchronous_Dart_Future - Fatal编程技术网

Api 无法在itemCount中获取数据的长度

Api 无法在itemCount中获取数据的长度,api,flutter,asynchronous,dart,future,Api,Flutter,Asynchronous,Dart,Future,我想获取要在Listview.builder小部件中使用的API数据的长度。我想从“mahalle”API获取数据。这个数据是一个数据列表。我想得到这个数据的长度来建立一个列表。但我犯了这样一个错误: #4 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:4546 ... ══════════════════════════════════════════════════════════

我想获取要在Listview.builder小部件中使用的API数据的长度。我想从“mahalle”API获取数据。这个数据是一个数据列表。我想得到这个数据的长度来建立一个列表。但我犯了这样一个错误:

#4      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:4546
...
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
Class 'Future<dynamic>' has no instance getter 'length'.
Receiver: Instance of 'Future<dynamic>'
Tried calling: length
The relevant error-causing widget was
    MahalleList 

错误的参考值为:

@action
  Future<void> fetcMahalle() async {
    // var data =
    //     await httpClient.getData(globals.SELECT_URL).then((mahalle) => mahalle);
    networkService.getMahalle();
  }

我通过以下方式获取数据:

Future getMahalle() async {
    BaseOptions options = new BaseOptions(
      baseUrl: globals.PROD_URL,
      connectTimeout: 5000,
      receiveTimeout: 3000,
    );
    Dio dio = new Dio(options);
    dio.options.headers["Authorization"] = "Bearer ${globals.USER_TOKEN}";
    try {
      var response =
          await dio.get(globals.SELECT_URL); //'api/hizlirapor/selects'

      List<MahalleModel> mahalleList = response.data['mahalle']
          .map<MahalleModel>((mahalle) => MahalleModel.fromJson(mahalle))
          .toList();

      return mahalleList;
    } on DioError catch (e) {
      debugPrint("ERRORR!!!!!!!!!!!!! ${e.error.toString()}");
      return null;
    }
  }
最后是我尝试使用列表数据长度的小部件:

Container _buildBody(
      BuildContext context, ObservableFuture<List<MahalleModel>> future) {
    return Container(
      color: backgroundColor,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          _buildSearchBar(context),
          RefreshIndicator(
            onRefresh: mahalleStore.fetcMahalle,
            child: ListView.builder(
                physics: const AlwaysScrollableScrollPhysics(),
                itemCount: mahalleList.length,
                itemBuilder: (context, index) {
                  final mahalle = mahalleList[index];
                  return Container(
                    height: 100,
                    child: Card(
                      color: Colors.white,
                      margin: EdgeInsets.all(15),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(
                          Radius.circular(10),
                        ),
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Container(
                            color: mainColor,
                            width: 3,
                            height: 50,
                          ),
                          SizedBox(
                            width: 15,
                          ),
                          Icon(
                            AppIcon.mahalle_raporu,
                            color: mainColor,
                          ),
                          SizedBox(
                            width: 15,
                          ),
                          Text(
                            mahalle.mahalleAdi,
                            style: textStyle,
                          ),
                        ],
                      ),
                    ),
                  );
                }),
          ),
        ],
      ),
    );
  }
谢谢你的帮助

您应该使用FutureBuilder,因为getMahalle返回未来。

您应该使用FutureBuilder,因为getMahalle返回未来

我删除了之前的评论,请检查此评论


我删除了以前的评论,请检查此评论。

您能否将Future getMahalle更改为Future getMahalle,因为它将返回没有长度的FutureBuilderdirty?您能否将Future getMahalle更改为Future getMahalle,因为它将返回没有长度的FutureBuilderdirty,状态:_FutureBuilderState74754:生成函数返回null。有问题的小部件是:FutureBuilder构建函数不能返回null。要返回导致建筑小部件填满可用空间的空白空间,请返回容器。要返回占用尽可能少空间的空白空间,请返回Containerwidth:0.0,height:0.0。导致错误的相关小部件是:FutureBuilder``我在您的建议中遇到了一个新错误:/。我设法把马哈列进去了。不幸的是,没有。我得到另一个错误,说我的FutureBuilder返回null。但是我成功地在getMahalle中打印了我的数据。“构建FutureBuilderdirty时抛出了以下断言,state:\u FutureBuilderState74754:构建函数返回null。有问题的小部件是:FutureBuilder构建函数不能返回null。要返回导致建筑小部件填满可用空间的空白空间,请返回容器。要返回占用尽可能少空间的空白空间,请返回Containerwidth:0.0,height:0.0。导致错误的相关小部件是:FutureBuilder``我在您的建议中遇到了一个新错误:/。我设法把马哈列进去了。不幸的是,没有。我得到另一个错误,说我的FutureBuilder返回null。但我设法在getMahalle中打印了我的数据。我尝试使用FutureBuilder,但得到的FutureBuilder构建函数不能返回null。要返回导致构建小部件填满可用空间的空白空间,请返回Container error.FutureBuilder具有一个应返回小部件的构建器函数。Docs:我尝试使用FutureBuilder,但我得到的FutureBuilder构建函数不能返回null。要返回导致构建小部件填满可用空间的空白空间,请返回Container error.FutureBuilder具有一个应返回小部件的构建器函数。文件:
FutureBuilder(
  future:mahalleStore.fetchMahalle,
  builder: (context, snapshot){
//whatever returns from this function, will be avaliable inside snapshot paremeter.
final mahalleList= snapshot.data;
switch (snapshot.connectionState) {
  case ConnectionState.waiting:
    {
      return Center(child: CircularProgressIndicator(),);
    }
  case ConnectionState.done:
    if (snapshot.hasData) {
      // do what you want here
    }
    return Text("Error occured");

  default:
    //
}});