Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在具有底部导航的应用程序中处理http GET请求_Http_Dart_Flutter - Fatal编程技术网

如何在具有底部导航的应用程序中处理http GET请求

如何在具有底部导航的应用程序中处理http GET请求,http,dart,flutter,Http,Dart,Flutter,我目前正在开发一个有多个页面的应用程序,使用底部导航栏,每个页面都需要向不同的API端点发送HTTP GET请求 现在我在每个页面的initState()内调用get函数。因此,每次我点击导航栏转到相应页面时,它都会再次发送另一个HTTP GET请求。我该怎么办?我应该从底部导航页面发送GET请求吗 我尝试过使用PageStorageKey方法,但我认为问题在于我在每个页面的initState中调用了GET方法 我的飞镖 bottomNavigationBar: Theme( data:

我目前正在开发一个有多个页面的应用程序,使用底部导航栏,每个页面都需要向不同的API端点发送HTTP GET请求

现在我在每个页面的initState()内调用get函数。因此,每次我点击导航栏转到相应页面时,它都会再次发送另一个HTTP GET请求。我该怎么办?我应该从底部导航页面发送GET请求吗

我尝试过使用PageStorageKey方法,但我认为问题在于我在每个页面的initState中调用了GET方法

我的飞镖

bottomNavigationBar: Theme(
    data: Theme.of(context).copyWith(
          // sets the background color of the `BottomNavigationBar`
          canvasColor: Color(0xff3a3637),
          // sets the active color of the `BottomNavigationBar` if `Brightness` is light
          primaryColor: Color(0xffffd51e),
          textTheme: Theme.of(context).textTheme.copyWith(
                caption: TextStyle(color: Colors.white),
              ),
        ), // sets the inactive color of the `BottomNavigationBar`

    child: BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: currentTab,
      onTap: (int index) {
        setState(() {
          currentTab = index;
          currentPage = pages[index];
        });
      },

      items: <BottomNavigationBarItem>[

        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icon/anggota_white.png")),
          title: Text(
            'Anggota',
            style: TextStyle(fontFamily: 'MyriadPro'),
          ),
        ),

        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icon/bk_white.png")),
          title: Text(
            "BK",
            style: TextStyle(fontFamily: 'MyriadPro'),
          ),
        ),

        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icon/himatif_white.png")),
          title: Text(
            "Himatif",
            style: TextStyle(fontFamily: 'MyriadPro'),
          ),
        ),

        BottomNavigationBarItem(
          icon: Icon(Icons.search),
          title: Text(
            "Cari",
            style: TextStyle(fontFamily: 'MyriadPro'),
          ),
        ),

        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icon/kkm_white.png")),
          title: Text(
            "KKM",
            style: TextStyle(fontFamily: 'MyriadPro'),
          ),
        ),
      ],
    ),
  ),
bottomNavigationBar:主题(
数据:Theme.of(context).copyWith(
//设置“底部导航栏”的背景色`
画布颜色:颜色(0xff3a3637),
//如果“亮度”为浅,则设置“底部导航栏”的活动颜色
原色:颜色(0xffffd51e),
textTheme:Theme.of(context).textTheme.copyWith(
描述:TextStyle(颜色:Colors.white),
),
),//设置“BottomNavigationBar”的非活动颜色`
子项:底部导航栏(
类型:BottomNavigationBarType.fixed,
currentIndex:currentTab,
onTap:(int索引){
设置状态(){
currentTab=索引;
currentPage=页面[索引];
});
},
项目:[
底部导航气压计(
图标:ImageIcon(AssetImage(“assets/icon/anggota_white.png”),
标题:正文(
“Anggota”,
样式:TextStyle(fontFamily:'MyriadPro'),
),
),
底部导航气压计(
图标:ImageIcon(AssetImage(“assets/icon/bk_white.png”),
标题:正文(
“BK”,
样式:TextStyle(fontFamily:'MyriadPro'),
),
),
底部导航气压计(
图标:ImageIcon(AssetImage(“assets/icon/himatif_white.png”),
标题:正文(
“Himatif”,
样式:TextStyle(fontFamily:'MyriadPro'),
),
),
底部导航气压计(
图标:图标(Icons.search),
标题:正文(
“Cari”,
样式:TextStyle(fontFamily:'MyriadPro'),
),
),
底部导航气压计(
图标:ImageIcon(AssetImage(“assets/icon/kkm_white.png”),
标题:正文(
“KKM”,
样式:TextStyle(fontFamily:'MyriadPro'),
),
),
],
),
),
这是这一页上的一个

安戈塔斯格林省

class AnggotaScreen extends StatefulWidget {
  AnggotaScreen({
    Key key,
  }) : super(key: key);

  @override
  _AnggotaScreenState createState() => _AnggotaScreenState();
}

class _AnggotaScreenState extends State<AnggotaScreen> {
  bool _isLoading;
  var _dataAngkatan, _dataTahun;
  static String _uriAngkatan;

  _ambilData(String url, bool tipe) async {
    final response = await http.get(url);

    if (response.statusCode == 200) {
      final map = json.decode(response.body);

      if (tipe == true) {
        setState(() {
          _dataAngkatan = map;
          _isLoading = false;
        });
      } else {
        setState(() {
          _dataTahun = map;
          _isLoading = false;
        });
      }
    }
  }

  // initState
  @override
  void initState() {
    super.initState();
    _isLoading = true;
    _uriAngkatan = "2012";
    _dataAngkatan = [];
    _dataTahun = [];
    _ambilData(Url.TAHUN_ANGGOTA, false);
    _ambilData(Url.angkatan(_uriAngkatan), true);
  }
  ..........
}
类AnggotaScreen扩展StatefulWidget{ 昂戈塔斯格林({ 关键点, }):super(key:key); @凌驾 _AngGotasScreenState createState()=>\u AngGotasScreenState(); } 类_AnggotaScreenState扩展状态{ bool_卸载; var(var)dataAngkatan,(var)dataTahun ;; 静态字符串_uriAngkatan; _ambilData(字符串url,bool tipe)异步{ 最终响应=等待http.get(url); 如果(response.statusCode==200){ final map=json.decode(response.body); 如果(tipe==true){ 设置状态(){ _大唐加丹=地图; _isLoading=false; }); }否则{ 设置状态(){ _dataTahun=地图; _isLoading=false; }); } } } //初始状态 @凌驾 void initState(){ super.initState(); _isLoading=true; _uriAngkatan=“2012”; _大唐加丹=[]; _dataTahun=[]; _ambilData(Url.TAHUN_ANGGOTA,false); _ambilData(Url.angkatan(_uriAngkatan),true); } .......... }
我想让页面在开始时只发送一次GET请求,并保持该状态直到应用程序关闭,但现在每次我打开该页面时,它都会发送GET请求

我认为你是对的。我开发了一个类似情况的应用程序,我在父页面的initState(在您的示例中是底部导航页面)发送了所有http请求,以避免发送多个http请求。只需发送请求并在父页面保存数据,然后将这些数据向下传递到每个子页面

Offstage
一个小部件,它将孩子放在树中,但不绘制任何内容,不让孩子进行命中测试,也不占用家长的任何空间。
TickerMode
可用于禁用子树中的动画

body:Stack(
儿童:[
新后台(
后台:索引!=0,
孩子:新的TickerMode(
已启用:索引==0,
子项:Screen()//MainScreen(),
),
),
新后台(
后台:索引!=1,
孩子:新的TickerMode(
已启用:索引==1,
子:Screen(),
),
),
新后台(
后台:索引!=2,
孩子:新的TickerMode(
已启用:索引==2,
子:Screen(),
),
),
新后台(
后台:索引!=3,
孩子:新的TickerMode(
已启用:索引==3,
子:Screen(),
),
),
],
)
body: Stack(
              children: <Widget>[
                new Offstage(
                  offstage: index != 0,
                  child: new TickerMode(
                      enabled: index == 0,
                      child: Screen() //MainScreen(),
                      ),
                ),
                new Offstage(
                  offstage: index != 1,
                  child: new TickerMode(
                    enabled: index == 1,
                    child: Screen(),
                  ),
                ),
                new Offstage(
                  offstage: index != 2,
                  child: new TickerMode(
                    enabled: index == 2,
                    child: Screen(),
                  ),
                ),
                new Offstage(
                  offstage: index != 3,
                  child: new TickerMode(
                    enabled: index == 3,
                    child: Screen(),
                  ),
                ),
              ],
            )