android - Flutter - 首次构建时出现 FutureBuilder 错误
问题描述
我正在尝试构建一个应用程序,该应用程序在调用数据库后显示一个列表。但是当我第一次启动该应用程序时,总会有我的错误消息。我在此处发布此内容的原因是:我希望该列表即使在首次启动时也能显示出来,这样甚至不会显示错误消息。
这是列表的代码:
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:side_header_list_view/side_header_list_view.dart';
import 'package:recipe/database/database.dart';
import 'package:recipe/interface/GoogleColors.dart';
import 'package:recipe/model/Recipes.dart';
import 'package:recipe/recipe/recipeDetails.dart';
Future<List<Recipes>> fetchRecipes() async{
var dbHelper = DBHelper();
Future<List<Recipes>> recipes = dbHelper.getRecipes();
return recipes;
}
class Lists extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return _List();
}
}
class _List extends State<Lists>{
DBHelper db = new DBHelper();
GoogleMaterialColors colors = new GoogleMaterialColors();
Random random = new Random();
Color usedColor;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Container(
alignment: Alignment.topCenter,
child: new FutureBuilder<List<Recipes>>(
future: fetchRecipes(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return new Text("Zurzeit sind keine Daten vorhanden."); //this error shows up
}
else if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index){
return new Text(snapshot.data[index].name);
},
);
}
return new Container(alignment: AlignmentDirectional.center,child: new CircularProgressIndicator(),);
},
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xFF0F9D58),
elevation: 4.0,
child: Icon(Icons.add),
onPressed: (){
Navigator.pushNamed(context, '/add_recipe');
},
)
);
}
void showBottomSnack(String value, ToastGravity toastGravity){
Fluttertoast.showToast(
msg: value,
toastLength: Toast.LENGTH_SHORT,
gravity: toastGravity,
timeInSecForIos: 2,
);
}
}
DBHelper.getRecipes() 的代码:
Future<List<Recipes>> getRecipes() async{
List<Map> list = await _db.rawQuery("SELECT * FROM recipes");
List<Recipes> recipes = new List();
for(int i =0; i < list.length; i++){
recipes.add(new Recipes(id: list[i]["id"],name: list[i]["name"],definition: list[i]["definition"],duration: list[i]["duration"], favorite: list[i]["favorite"], timestamp: list[i]["timestamp"], image: list[i]["image"],backgroundColor: list[i]["backgroundColor"]));
}
return recipes;
}
食谱类别:
class Recipes{
int id, favorite;
dynamic image;
String name, definition, timestamp, duration, backgroundColor;
Recipes(
{
@required this.id,
this.name,
this.definition,
this.duration,
this.favorite,
this.timestamp,
this.image,
this.backgroundColor
}
);
}
snapshot.error 返回:
NoSuchMethodError:在 null 上调用了方法“rawQuery”。接收方:null 尝试调用:rawQuery('SELECT * FROM recipes')
解决方案
推荐阅读
- ios - 当我为我的 UITextField 打开安全文本输入时,文本是不可见的
- odoo - Odoo,如何覆盖扩展其域的操作(不覆盖)
- r - 将带有“##”标签的文本数据读取为 data.frame
- github - 只有在输入“用户名”和“github”时才能在 google 上找到 GitHub 页面
- java - FileWrite 没有创建新文件 Android Studio,得到 'java.io.FileNotFoundException: file.py: open failed: EROFS (Read-only file system)'
- git - 如何在 Bitbucket 中的特定提交之前删除所有提交
- python - 计算具有特定列名的列中的值
- javascript - 如何在 vue js 中动态绑定到 img 的 :src?
- vue.js - 如何使用 vue-3d-model 在 electron-vue webpack 项目中导入 .obj
- javascript - 如何始终在 div 上显示垂直滚动条