Вы можете скопировать и вставить полный код ниже
Я использую задержку в 3 секунды для имитации сетевого вызова
Шаг 1: Удалите setState(() => this.context = context);
из build
Шаг 2: Используйте addPostFrameCallback
для вызова _orderList()
дюйм initState
@override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_orderList();
});
}
рабочая демонстрация
введите описание изображения здесь
полный код
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
List<OrderModel> orderModelFromJson(String str) =>
List<OrderModel>.from(json.decode(str).map((x) => OrderModel.fromJson(x)));
String orderModelToJson(List<OrderModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class OrderModel {
OrderModel({
this.neworder,
this.address,
this.type,
this.customerName,
});
String neworder;
String address;
String type;
String customerName;
factory OrderModel.fromJson(Map<String, dynamic> json) => OrderModel(
neworder: json["neworder"],
address: json["address"],
type: json["type"],
customerName: json["customerName"],
);
Map<String, dynamic> toJson() => {
"neworder": neworder,
"address": address,
"type": type,
"customerName": customerName,
};
}
void main() => runApp(OrderScreen());
class OrderScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyOrderPage(title: 'Flutter Demo Home Page'),
);
}
}
class MyOrderPage extends StatefulWidget {
@override
MyOrderPage({Key key, this.title}) : super(key: key);
final String title;
@override
OrderScreenState createState() => OrderScreenState();
}
class OrderScreenState extends State<MyOrderPage> {
Icon cusIcon = Icon(Icons.search);
Widget cusSearchBar = Text("Order");
bool _isLoading = false;
final String sUrl = "https://fasttracksoft.us/api_v2/device_installation/";
List<OrderModel> orderList;
BuildContext context;
Future<List<OrderModel>> _orderList() async {
setState(() {
_isLoading = true;
});
/*final prefs= await SharedPreferences.getInstance();
var params = "myaccount.php?id="+prefs.getString("id")+"&type=" +"order";
print(params);*/
try {
//final res =await http.get(sUrl+params);
//print(res.body);
String jsonString = '''
[
{
"neworder" : "order 1",
"address" : "abc",
"type" : "def",
"customerName": "aaa"
},
{
"neworder" : "order 2",
"address" : "abc1",
"type" : "def1",
"customerName": "aaa1"
}
]
''';
await Future.delayed(Duration(seconds: 3), () {});
final res = http.Response(jsonString, 200);
if (res.statusCode == 200) {
orderList = orderModelFromJson(res.body);
if (orderList != null) {
setState(() {
_isLoading = false;
});
}
}
} catch (e) {}
return orderList;
}
@override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_orderList();
});
}
@override
Widget build(BuildContext context) {
// TODO: implement build
//setState(() => this.context = context);
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
/*Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);*/
},
),
title: cusSearchBar,
actions: <Widget>[
IconButton(
onPressed: () {
setState(() {
if (this.cusIcon.icon == Icons.search) {
this.cusIcon = Icon(Icons.cancel);
this.cusSearchBar = TextField(
textInputAction: TextInputAction.go,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search here",
),
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
);
} else {
this.cusIcon = Icon(Icons.search);
this.cusSearchBar = Text("AppBar");
}
});
},
icon: cusIcon,
),
],
),
body: ListView.builder(
itemCount: null == orderList ? 0 : orderList.length,
itemBuilder: (context, index) {
OrderModel orderModel = orderList[index];
return Padding(
padding: const EdgeInsets.only(
top: 0.0, bottom: 0.0, left: 5.0, right: 5.0),
child: GestureDetector(
//onTap: ()=> Navigator.push(context, new MaterialPageRoute(builder: (context)=> new OrderCountDetails())),
child: Card(
elevation: 10,
child: Container(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 15.5,
color: Colors.green,
padding: const EdgeInsets.only(
top: 10.0, bottom: 10.0, left: 15.0, right: 0.0),
child: Text(
"NEW ORDER",
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
new Container(
padding: const EdgeInsets.only(
top: 10.0, bottom: 0.0, left: 10.0, right: 10.0),
child: Text(
'Customer Name :' +
orderModel.customerName.toString(),
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
),
new Container(
padding: const EdgeInsets.only(
top: 0.0, bottom: 0.0, left: 10.0, right: 10.0),
child: Text(
'Type :' + orderModel.type.toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
),
),
new Container(
padding: const EdgeInsets.only(
top: 0.0, bottom: 0.0, left: 10.0, right: 10.0),
child: Text(
'Total Devices :',
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
),
),
new Container(
padding: const EdgeInsets.only(
top: 0.0, bottom: 0.0, left: 10.0, right: 10.0),
child: Text(
'Address :' + orderModel.address.toString(),
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
),
),
new Container(
padding: const EdgeInsets.only(
top: 0.0, bottom: 10.0, left: 10.0, right: 10.0),
child: Text(
'New Orders Count :' + orderModel.neworder,
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
),
),
],
),
),
),
),
);
},
),
);
}
}