Flutter-SQFlite和FutureBuilder快照返回空列表

时间:2020-07-28 12:36:29

标签: flutter dart future sqflite

我正在尝试使用搜索功能,在该功能中,用户输入单词并获取单词列表作为回报(用户输入:“ Hello”,结果列表:“ Hei”,“ Hallo”),反之亦然,返回的字符串列表始终以0的大小返回。

这是我的模特

class Word {
  final int wordId;
  final String english;
  final String norwegian;
  final String isFav;

  Word({this.wordId, this.english, this.norwegian, this.isFav});

  Map<String, dynamic> toMap() {
    return {
      'wordId': wordId,
      'englishWord': eng,
      'norwegianWord': nor,
      'isFavorite': isFav,
    };
  }

  factory Word.fromMap(Map<String, dynamic> json) => new Word(
      wordId: json['wordId'],
      english: json['englishWord'],
      norwegian: json['norwegianWord'],
      isFav: json['isFavorite']);
}

主要:

bool isNorwegian = false; //true when user selects to search in Norwegian, false by default.
List<String> mResultArray = new List();

//...irrelevant code

class SearchBar extends StatefulWidget {
  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {

    DatabaseHelper databaseHelper;
    
    @override
    void initState() {
        super.initState();
        databaseHelper = DatabaseHelper.databaseHelper;
    }
    
    
    search(String q){
        setState((){
            FutureBuilder<List<Word>>(
                future: databaseHelper.getAllwords(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData) {
                        mSearchResultList = snapshot.data;
                        mSearchResultList.forEach((element) {
                          if (isNorwegian == true) {
                              if (element.norwegian == q) {
                                  mResultArray.add(element.eng);
                              }
                          } else if (isNorwegian == false) {
                              if (element.english == StringUtils.capitalize(q) {
                                 mResultArray.add(element.norwegian);
                              }
                           }
                       });
                   }
                  return Container(child: CircularProgressIndicator());
             });
        });
    }
} 

0 个答案:

没有答案