Flutter-如何将数据从SQFlite表填充到数组列表变量?

时间:2019-05-06 15:51:12

标签: dart flutter

我不再想要使用FutureBuilder,因为我试图在这方面寻求帮助,但没有运气。我希望能够使用SQflite ONCE从数据库中的表中加载数据并将其复制到数组列表中。我不知道该怎么做。

这里是情况...看起来像这样:

enter image description here

在我按下Airport下拉菜单后(此下拉菜单是唯一使用SQFlite中表格数据的菜单:

enter image description here

从“机场名称”列表中选择一个项目后...产生此错误:

enter image description here

我尝试使用FutureBuilder时寻求帮助,并且每次我从dropdownmenu菜单中选择某些内容或对小部件进行任何更改时,它都会继续从表中检索所有数据。我只想一次加载它,并让dropdownmenu使用数组列表变量而不是SQFLite中的列表。

在下面的customscreen.dart中的代码中..您看到了将来:每次小部件发生更改时,都会触发db.getAllAirports()。第一次在下拉菜单中列出所有机场名称都很好。一旦我在列表中选择了一个机场名称,它便启动了db.getAllAirports()并出现以下错误:

flutter (13027): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (13027): The following assertion was thrown building FutureBuilder<List<AirportModel>>(dirty, state:
I/flutter (13027): _FutureBuilderState<List<AirportModel>>#92899):
I/flutter (13027): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null ||
I/flutter (13027): items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value ==
I/flutter (13027): value).length == 1': is not true.
I/flutter (13027): 
I/flutter (13027): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (13027): more information in this error message to help you determine and fix the underlying cause.
I/flutter (13027): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (13027):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (13027): 

dbhandler.dart

  Future<List<AirportModel>> getAllAirports() async {
        print("=========== Entered getAllAirports========");
        var dbAirport = await db;
        String sql;
        sql = "SELECT * FROM airportTable";

        var result = await dbAirport.rawQuery(sql);

        if (result.length == 0) return null;

        List<AirportModel> list = result.map((item) {
         return AirportModel.fromMap(item);
        }).toList();
        print("**** before result on way out of  getAllAirpots ****");
        print(result);
        print("list is $list");
        return list;

     // });
  }

customscreen.dart

           child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                  FutureBuilder<List<AirportModel>>(
                    future: db.getAllAirports(),
                    builder: (BuildContext context, AsyncSnapshot<List<AirportModel>> snapshot) {
                      if (!snapshot.hasData) return CircularProgressIndicator();
                       return DropdownButton<AirportModel>(items: snapshot.data.map((airportItem) =>
                            DropdownMenuItem<AirportModel>(
                              child: Text(airportItem.airportName),
                              value: airportItem,
                            ))
                            .toList(),
                        onChanged: (AirportModel value) {
                          setState(() {
                            if (snapshot.hasData) debugPrint(
                                "--------- snapshot is ${snapshot.data}");
                            _currentAirport = value;
                            selectedAirport = _currentAirport.airportName;
                          });
                        },
                        value: _currentAirport,
                        hint: Text("Airport"),
                      );
                    }),

我想做的是避免使用Futurebuilder,而只是将它们复制到数组列表变量中。然后,我可以使用下拉菜单在列表中使用这些项目。我该怎么做呢?我还是Flutter&Dart的新手。

0 个答案:

没有答案