'How to solve moor error with provide and buildcontext?
Error provider with moor database still appears after adding provide as well.
After click on Save
Widget buildButton() {
final isFormValid = title.isNotEmpty && minutesPerDay.isNotEmpty && timeGoal.isNotEmpty;
return Padding(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
onPrimary: Colors.white,
primary: isFormValid ? null : Colors.grey.shade700,
),
onPressed: addOrUpdateGoal,
child: Text('Save'),
),
);
}
addOrUpdate uses
Future addGoal() async {
final database = Provider.of<AppDatabase>(context, listen: false);
final goal = Goal(
id: 0,
goal: timeGoal,
intenseGoal: minutesPerDay,
timeGoal: timeGoal,
initialDate: initialDate,
editDate: editDate,
completed: isImportant,
color: color,
);
database.insertGoal(goal);
}
Error appears and nothing is added to database:
Error: Could not find the correct Provider<AppDatabase> abov
e this AddEditGoalPage Widget
E/flutter (29178): - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
E/flutter (29178):
E/flutter (29178): Make sure that AddEditGoalPage is under your MultiProvider/Provider<AppDatabase>.
E/flutter (29178): This usually happens when you are creating a provider and trying to read it immediately.
added this lines:
return Provider(
create: (_) => AppDatabase(),
child: Scaffold(
class AddEditGoalPage extends StatefulWidget {
final Goal? goal;
const AddEditGoalPage({
Key? key,
this.goal
}) : super(key: key);
@override
_AddEditGoalPageState createState() => _AddEditGoalPageState();
}
// TO EDIT values
class _AddEditGoalPageState extends State<AddEditGoalPage> {
final _formKey = GlobalKey<FormState>();
late bool isImportant;
late String timeGoal;
late String title;
late String minutesPerDay;
late String color;
late String initialDate;
late String editDate;
get database => AppDatabase;
@override
void initState() {
super.initState();
// to check
isImportant = widget.goal?.completed ?? false;
timeGoal = widget.goal?.timeGoal ?? '';
title = widget.goal?.goal ?? '';
minutesPerDay = widget.goal?.intenseGoal ?? '';
color = widget.goal?.color ?? '';
initialDate = widget.goal?.initialDate ?? '';
editDate = widget.goal?.editDate ?? '';
}
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => AppDatabase(),
child: Scaffold(
appBar: AppBar(
actions: [buildButton()],
),
body: Form(
key: _formKey,
child: GoalFormWidget(
isImportant: isImportant,
timeGoal: timeGoal,
title: title,
minutesPerDay: minutesPerDay,
onChangedImportant: (isImportant) =>
setState(() => this.isImportant = isImportant),
onChangedTitle: (title) => setState(() => this.title = title),
onChangedMinutesPerDay: (minutesPerDay) =>
setState(() => this.minutesPerDay = minutesPerDay),
onChangedDaysForGoal: (timeGoal) =>
setState(() => this.timeGoal = timeGoal),
),
),
)
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
