Scoped Model - not working after go back to previous page
syazie opened this issue · 0 comments
When I open the app. The app will call _callAPI() async {} since I put it in init. Then I click counter++ to add the value. Then it will show the added value in screen.then submit go to next page. when I click back page. The page will call the API then suddenly my scope model not working. when I try add value. The value not change.when I print in debug console. It did added the value.but It does not change in my screen. I think the problem is my scope model not working anymore in my app.
main.dart`class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
final MainModel _model = MainModel();
return ScopedModel<MainModel>(
model: _model,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class _MyHomePageState extends State {
@OverRide
void initState(){
super.initState();
fetchList().then((value){
setState(() {
_callAPI();
});
});
}
_callAPI() async {
try{
String dataURL = "url";
http.Response response = await http.get(dataURL);
}
on Exception{
}
}
Widget build(BuildContext context) {
return ScopedModelDescendant(
builder: (BuildContext context, Widget child, MainModel model) {
return Scaffold(
appBar: AppBar(
title: appBarTitle,
centerTitle: true,
actions: [
Stack(
children: [
new IconButton(icon: Icon(Icons.card_travel), onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => cartPage()),
);
}),
Container(
child: model.list.length != 0 ? new Positioned(
right: 11,
top: 11,
child: new Container(
padding: EdgeInsets.all(2),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 16,
minHeight: 16,
),
child: Text(
"${model.list.length}",
style: TextStyle(
color: Colors.white,
fontSize: 12,
),
textAlign: TextAlign.center,
),
),
) : new Container()
),
],
),
],
backgroundColor: getMainColor(),
elevation: 10.0,
),
drawer: new Drawer(
child:
_drawerLeft(context),
),
body:
Container(
),
);}
);}
Widget addValue{
return Padding(
padding: EdgeInsets.fromLTRB(9, 0, 0, 0),
child: SizedBox (
height:35,
width:35,
child: FloatingActionButton(
heroTag: "addM",
onPressed: () {
Map setValue = {"var1 " : var1};
model.addInList(setValue);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => nextPage()),
);
},
child: Icon(
Icons.add_circle_outline,
color: Colors.white,
size: 19,
),
backgroundColor: getMainColor(),
),
),
)
}
}`
scoped_models\main.dart
`List _list=[];
List get list{
return _list;
}
void addInList(Map newList){
_list.insert(0, newList);
notifyListeners();
}
`