我正在制作一个应用程序。我从URL获取数据并尝试显示。但是我需要重新加载才能看到。
这些是我的代码:
import 'package:flutter/material.dart';
import 'widgets/half_category_widget.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
var searchCat;
var baslik="Ana Sayfa";
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
home: FirstApp(),
));
createCategory();
createProduct();
}
void runCat(id){
searchCat=id+1;
searchProds();
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
home: CatProds(),
));
}
class FirstApp extends StatefulWidget {
@override
_FirstAppState createState() => _FirstAppState();
}
List<Widget> menu = new List<Widget>();
Future createCategory() async{
var url = 'https://recepabi.com/pc/getCatKafe';
http.Response response = await http.get(url);
var data = (response.body).toString().split('-');
for(var i=0;i<data.length-1;i++){
menu.add(Expanded(
child: Padding(
padding: const EdgeInsets.all(4.0),
child:
InkWell(onTap: () {
print("ID:" + i.toString());
runCat(i);
baslik=data[i];
},
child: Container(
height: 100.0,
width: 100.0,
child: Column(
children: <Widget>[
HalfCategory(Colors.blue, Icons.menu, data[i]),
],
),
),
) ,
),
));
}
}
List<Widget> products = new List<Widget>();
List<Widget> products1 = new List<Widget>();
Future createProduct() async{
var url = 'https://recepabi.com/pc/getProdKafe';
http.Response response = await http.get(url);
var data = (response.body).toString().split('ğ');
for(var i=0;i<data.length-1;i++){
var prod=data[i].split("ı");
var ad=prod[0];
var birim=prod[1];
var kategori=prod[2];
var fiyat=prod[3];
var resim=prod[4];
var myr=Row(children: <Widget>[Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 5.0, right: 5.0 ),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: Image.network(
resim,
fit: BoxFit.cover,
repeat: ImageRepeat.noRepeat,
height: 200.0,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0,bottom: 16.0),
child: Text(
ad.toUpperCase(),
maxLines: 1,
style: TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
)
],
),
),
),],);
products1.add(myr);
}
}
class _FirstAppState extends State<FirstApp> {
int _index = 0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Kafeex"),
centerTitle: true,
backgroundColor: Colors.red,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _index,
onTap: (int index) {
setState(() {
_index = index;
if(_index==0)
{
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => FirstApp()),
(Route<dynamic> route) => false,
);
}
else if(_index==1)
{
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
(Route<dynamic> route) => false,
);
}
});
},
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.fastfood),
title: new Text('Sipariş'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.shopping_cart),
title: new Text('Sepet'),
),
],
),
body: Padding(
padding: const EdgeInsets.all(2.0),
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
Row(
children:menu
),
SizedBox(
height: 8.0,
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Row(
children: <Widget>[Expanded(
child: Text(baslik,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.start,
),
),],
),
),
SizedBox(
height: 10.0,
),
Column(
children: products1
),
],
),
)
);
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}
class CatProds extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Kafeex"),
centerTitle: true,
backgroundColor: Colors.red,
),
body: Padding(
padding: const EdgeInsets.all(2.0),
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
Row(
children:menu
),
SizedBox(
height: 8.0,
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Row(
children: <Widget>[Expanded(
child: Text(baslik,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.start,
),
),],
),
),
SizedBox(
height: 10.0,
),
Column(
children: searchProd
),
],
),
)
);
}
}
List<Widget> searchProd = new List<Widget>();
Future searchProds() async{
var url = 'https://recepabi.com/pc/searchProdKafe/'+searchCat.toString();
print(url);
http.Response response = await http.get(url);
var data = (response.body).toString().split('ğ');
for(var i=0;i<data.length-1;i++){
var prod=data[i].split("ı");
var ad=prod[0];
var birim=prod[1];
var kategori=prod[2];
var fiyat=prod[3];
var resim=prod[4];
var myr2=Row(children: <Widget>[Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 5.0, right: 5.0 ),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: Image.network(
resim,
fit: BoxFit.cover,
repeat: ImageRepeat.noRepeat,
height: 200.0,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0,bottom: 16.0),
child: Text(
ad.toUpperCase(),
maxLines: 1,
style: TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
)
],
),
),
),],);
searchProd.add(myr2);
searchCat=0;
}
}
例如:我从URL(createCategory())获取数据。我将数据放入列表(菜单),并尝试显示在小部件(_FirstAppState的小部件)上。但是首先,什么也没有出现。在第二次运行中(通过热重装)出现。 我该如何预防?
答案 0 :(得分:0)
这里的问题是您在createCategory();
函数中调用createProduct();
和main()
函数,如果您在这些方法中更改了一些代码,则需要再次调用main()
方法,这不是可能会在热重装时改变状态和一些小部件
因此要再次调用main()
方法,您需要执行热重启