API (スクリーン 1) からデータをロードしています。項目をクリックすると、新しいステートフル ウィジェットで新しいスクリーン (スクリーン 2) が開きます。しかし、画面2から戻る矢印を押すと、画面1が表示されますが、APIを再度呼び出しています。画面 2 から画面 1 に戻るときは、そうしたくありません。
画面 1 コード:
class Screen1 extends StatefulWidget {
@override
_Screen1State createState() => _Screen1State();
}
class _Screen1 State
extends State<Screen1> with AutomaticKeepAliveClientMixin<Screen1>
{
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getdata(), //api calling
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
// TODO: Handle this case.
case ConnectionState.waiting:
//my code
case ConnectionState.active:
// TODO: Handle this case.
case ConnectionState.done:
if (snapshot.connectionState == ConnectionState.done) {
return showdata(snapshot.data);
}
}
});
}
Widget showdata(Response data) {
return ListView.builder(
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: BoxDecoration(color: Colors.white),
child: ListTile(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (_) => Screen2()));
},
)
);
});
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
画面 2 :
class Screen2 extends StatefulWidget {
@override
_Screen2State createState() => _Screen2State();
}
class _Screen2State extends State<Screen2> {
@override
Widget build(BuildContext context) {
return Container();
}
}
画面 2 から戻ったときに画面 1 を再構築しないようにするにはどうすればよいですか?