I was working in a simple notes app in flutter, this is my code.
i just made this example to preview the problem
This is the home page that navigate to a create page, and uses a futureBuilder to display the data from the database
import 'package:flutter/material.dart';
import 'package:futurebuilder_test/db.dart';
import 'create.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final DatabaseHelper db = DatabaseHelper.instance;
Future futureData;
@override
void initState() {
futureData = _getData();
super.initState();
}
Future _getData() async{
return await db.queryAllRows('notes');
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => _navigateToCreatePage(context),
),
body: FutureBuilder(
future: _getData(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) {
return ListTile(
title: Text(snapshot.data[index]['title']),
);
},
);
}
return Text('Empty');
},
),
);
}
}
void _navigateToCreatePage(context) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CreatePage())
);
}
and this is the create page
import 'package:flutter/material.dart';
import 'package:futurebuilder_test/db.dart';
class CreatePage extends StatelessWidget {
final DatabaseHelper db = DatabaseHelper.instance;
final TextEditingController title = TextEditingController();
final TextEditingController description = TextEditingController();
void save(context) async {
await db.insert(
'notes', {'title': title.text, 'description': description.text}
);
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
decoration: InputDecoration(hintText: 'Title'),
controller: title,
),
TextField(
decoration: InputDecoration(hintText: 'Title'),
controller: description,
),
RaisedButton(
child: Text('Save'),
onPressed: () => save(context),
)
],
),
),
);
}
}
when the user click the save button it adds a new note to the database and pop the page back to home screen It's supposed that the future builder update it's data after submitting new data to the database but this is not happening. could someone help me.