I had a custom class called Trans
(short for transaction) that stores a bool and a double and displays them in a Row
, I stored Trans
in a List transactions
and displayed it in page page_2
using ListView
(originally Trans was inside page_2) and saved transactions
using shared preferences, but then I moved my Trans
class and List Transactions
to a new file and made it a provider to be able to access Trans
and transactions
and add new Trans to the list in multiple places , I also moved Saving and Loading to the new Trans page and they stopped working and I get an error :
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: dependOnInheritedWidgetOfExactType<_InheritedProviderScope<Trans>>() or dependOnInheritedElement() was called before _pg2.initState() completed.
E/flutter ( 9102): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
E/flutter ( 9102): Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.
below is the entire source code for Trans
and page_2
without widget build
:
Trans:
class Trans extends StatelessWidget with ChangeNotifier {
SharedPreferences sharedPreferences;
final BackColor = const Color(0xff141414); // color for buttons
final text_color = const Color(0xffFFFFFF);
List <Trans> transactions = [
];
bool send;
Color color;
double amount;
Trans({Key key, this.send, this.amount}) : super(key: key);
Trans.fromMap(Map map)
: this.send = map['send'],
this.amount = map['amount'];
Map toMap() {
return {
'send': this.send,
'amount': this.amount,
};
}
@override
Widget build(BuildContext context) {...}
addTrans(){
transactions.add(Trans(send: this.send, amount: this.amount,));
notifyListeners();
}
void saveData() {
List<String> stringList = transactions.map(
(item) => json.encode(item.toMap()
)).toList();
sharedPreferences.setStringList('list', stringList);
notifyListeners();
}
void loadData() {
List<String> listString = sharedPreferences.getStringList('list');
if(listString != null){
transactions = listString.map(
(item) => Trans.fromMap(json.decode(item))
).toList();
notifyListeners();
}
}
void clearData() async{
transactions.clear();
saveData();
}
}
page_2 :
class pg2 extends StatefulWidget with ChangeNotifier {
@override
_pg2 createState() => _pg2();
}
class _pg2 extends State<pg2> with ChangeNotifier {
@override
void initState() {
initSharedPreferences();
super.initState();
}
initSharedPreferences() async{
final trans = Provider.of<Trans>(context, listen: true);
trans.sharedPreferences = await SharedPreferences.getInstance();
trans.loadData();
}
@override
final BackColor = const Color(0xff141414); // color for buttons
final text_color = const Color(0xffFFFFFF);
Widget build(BuildContext context) {
//only important thing here is a RaisedButton that adds new Trans to the list using addTrans()
RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()){
setState(() {
trans.addTrans();
trans.saveData();
});
}
},
child: const Text("Add Transaction"),
),
}
}
Remove with ChangeNotifier
on class pg2
. ChangeNotifier on _pg2
is sufficient
User contributions licensed under CC BY-SA 3.0