我正在尝试构建一个非常简单的应用笔记,并使用SQFlite软件包将笔记保存到本地数据库中。在我的代码中,我正在使用将来的版本来显示从数据库中加载数据时显示的CircularProgressIndicator,并且此刻db是空的,因此应用程序应显示一个空的主体。我的问题是数据永远不会加载,循环进度指示器是无限的。同样,“添加注释”按钮也将打开一个警报对话框,其中包含两个文本字段,该文本字段应接收文本,并将其从数据库插入表中,并且该文本应显示在正文中的Card中,但这不会发生。 我的代码:
main.dart
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'note_inherited_widget.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NoteInheritedWidget(
MaterialApp(
home: HomeScreen(),
theme: ThemeData(
primaryColor: Colors.deepOrange, accentColor: Colors.deepPurple),
),
);
}
}
home_screen.dart
import 'package:flutter/material.dart';
import 'package:simple_notes/note_inherited_widget.dart';
import 'package:simple_notes/providers/note_providers.dart';
import './note_inherited_widget.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final TextEditingController titleController = TextEditingController();
final TextEditingController textController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
titleController.dispose();
textController.dispose();
super.dispose();
}
DummyDataProvider notes;
@override
void didChangeDependencies() {
super.didChangeDependencies();
notes = NoteInheritedWidget.of(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Notes'),
centerTitle: true,
),
body: FutureBuilder(
future: NoteProvider.getNoteList(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final notes = snapshot.data;
ListView.builder(
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
NoteTitle(notes[index]['title']),
NoteText(notes[index]['text'])
],
),
);
},
itemCount: notes.length,
);
}
return Center(child: CircularProgressIndicator());
}
),
bottomNavigationBar: (FlatButton(
onPressed: () {
showMyDialog(context);
},
child: Text('Add note'))));
}
Future<void> showMyDialog(
BuildContext context,
) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Column(
children: <Widget>[
TextField(
controller: titleController,
),
TextField(
controller: textController,
)
],
),
actions: <Widget>[
MaterialButton(onPressed: () {
final title = titleController.text;
final text = textController.text;
NoteProvider.insertNote({
'title': title,
'text': text
});
Navigator.pop(context);
},
child: Text('save')
)
],
);
});
}
}
class NoteTitle extends StatelessWidget {
final String title;
NoteTitle(this.title);
@override
Widget build(BuildContext context) {
return Text(
title,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
);
}
}
class NoteText extends StatelessWidget {
final String text;
NoteText(this.text);
@override
Widget build(BuildContext context) {
return Text(
text,
style: TextStyle(
color: Colors.grey.shade600,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
);
}
}
class SaveButton extends StatelessWidget {
final Function onPressed;
SaveButton(this.onPressed);
@override
Widget build(BuildContext context) {
return MaterialButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Save'),
);
}
}
note_providers.dart
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class NoteProvider {
static Database db;
static Future open() async {
db = await openDatabase(join(await getDatabasesPath(), 'notes.db'),
version: 1,
onCreate: (Database db, int version) async {
db.execute('''
CREATE TABLE notes(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
text TEXT NOT NULL
);
''');
}
);
}
static Future<List<Map<String, dynamic>>> getNoteList() async {
if (db == null) {
await open();
}
return await db.query('Notes');
}
static Future insertNote (Map<String, dynamic> note) async {
await db.insert('Notes', note);
}
}
如果您能弄清为什么会发生这种情况,那就太好了。我仍在学习颤动,因此无法找出问题所在。
谢谢。
答案 0 :(得分:0)
似乎您在return
附近缺少ListView.builder
:
return ListView.builder(
因为当前,即使将来完成,它也始终返回循环进度指示器。