Skip to content

Instantly share code, notes, and snippets.

@prateek-aher
Last active May 15, 2021 14:57
Show Gist options
  • Save prateek-aher/66257a90f3aafdc8521356a670dceed6 to your computer and use it in GitHub Desktop.
Save prateek-aher/66257a90f3aafdc8521356a670dceed6 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:http/http.dart';
import 'model.dart';
class HttpService {
final String postsUrl =
"https://test.dabbawala.ml/mobileapi/vendor/getallitems.php";
Future<List<Model>> getData() async {
Response res = await post(Uri.parse(postsUrl), body: {'vendorID': '115'});
if (res.statusCode == 200) {
List<dynamic> data = jsonDecode(res.body)['data'];
List<Model> models = data.map((e) => Model.fromJson(e)).toList();
return models;
}
}
}
import 'package:flutter/material.dart';
import 'package:new_gradient_app_bar/new_gradient_app_bar.dart';
import 'http_service.dart';
import 'model.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'API Demo',
theme: ThemeData(
// primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'API demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
HttpService service;
@override
void initState() {
super.initState();
service = HttpService();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: NewGradientAppBar(
title: Text(
widget.title,
style: TextStyle(color: Colors.black),
),
centerTitle: true,
gradient: LinearGradient(
colors: [
Colors.tealAccent,
Colors.yellowAccent,
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
body: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.brown,
Colors.red,
Colors.deepOrange,
Colors.pink,
],
stops: [0.1, 0.4, 0.6, 0.9],
),
),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return FutureBuilder(
future: service.getData(),
builder: (context, snapshot) {
return Text('${snapshot.data[index]['itemID']}');
},
);
},
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:new_gradient_app_bar/new_gradient_app_bar.dart';
import 'http_service.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'API Demo',
theme: ThemeData(
// primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'API demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
HttpService service;
@override
void initState() {
super.initState();
service = HttpService();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: NewGradientAppBar(
title: Text(
widget.title,
style: TextStyle(color: Colors.black),
),
centerTitle: true,
gradient: LinearGradient(
colors: [
Colors.tealAccent,
Colors.yellowAccent,
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
body: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.brown,
Colors.red,
Colors.deepOrange,
Colors.pink,
],
stops: [0.1, 0.4, 0.6, 0.9],
),
),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return FutureBuilder(
future: service.getData(),
builder: (context, snapshot) {
return Text('${snapshot.data[index]['itemID']}');
},
);
},
),
),
),
);
}
}
class Model {
String itemID;
String itemVendorid;
String itemName;
String itemCuisine;
String itemType;
String itemCategory;
String itemPrice;
String itemDescription;
String itemStatus;
String lastAvailibityDate;
String vendorID;
String vendorName;
String vendorEMail;
String vendorPassword;
String vendorPhone;
String vendorAddress;
String vendorLatitude;
String vendorLongitude;
String vendorGSTIN;
String vendorCuisine;
String vendorLogoid;
String vendorDeliverypartner;
String vendorTax;
String vendorPackingcharge;
String vendorCommission;
String vendorBrkfaststart;
String vendorBrkfastend;
String vendorLunchstart;
String vendorLunchend;
String vendorSnacksstart;
String vendorSnacksend;
String vendorDinnerstart;
String vendorDinnerend;
String vendorFCM;
String vendorStatus;
String iimID;
String iimItemid;
String iimImageid;
String iimStatus;
String imageID;
String imageName;
String imageStatus;
String cuisineID;
String cuisineName;
String cuisineStatus;
Model(
{this.itemID,
this.itemVendorid,
this.itemName,
this.itemCuisine,
this.itemType,
this.itemCategory,
this.itemPrice,
this.itemDescription,
this.itemStatus,
this.lastAvailibityDate,
this.vendorID,
this.vendorName,
this.vendorEMail,
this.vendorPassword,
this.vendorPhone,
this.vendorAddress,
this.vendorLatitude,
this.vendorLongitude,
this.vendorGSTIN,
this.vendorCuisine,
this.vendorLogoid,
this.vendorDeliverypartner,
this.vendorTax,
this.vendorPackingcharge,
this.vendorCommission,
this.vendorBrkfaststart,
this.vendorBrkfastend,
this.vendorLunchstart,
this.vendorLunchend,
this.vendorSnacksstart,
this.vendorSnacksend,
this.vendorDinnerstart,
this.vendorDinnerend,
this.vendorFCM,
this.vendorStatus,
this.iimID,
this.iimItemid,
this.iimImageid,
this.iimStatus,
this.imageID,
this.imageName,
this.imageStatus,
this.cuisineID,
this.cuisineName,
this.cuisineStatus});
factory Model.fromJson(Map<String, dynamic> json) => Model(
itemID: json['itemID'],
itemVendorid: json['itemVendorid'],
itemName: json['itemName'],
itemCuisine: json['itemCuisine'],
itemType: json['itemType'],
itemCategory: json['itemCategory'],
itemPrice: json['itemPrice'],
itemDescription: json['itemDescription'],
itemStatus: json['itemStatus'],
lastAvailibityDate: json['LastAvailibityDate'],
vendorID: json['vendorID'],
vendorName: json['vendorName'],
vendorEMail: json['vendorE-Mail'],
vendorPassword: json['vendorPassword'],
vendorPhone: json['vendorPhone'],
vendorAddress: json['vendorAddress'],
vendorLatitude: json['vendorLatitude'],
vendorLongitude: json['vendorLongitude'],
vendorGSTIN: json['vendorGSTIN'],
vendorCuisine: json['vendorCuisine'],
vendorLogoid: json['vendorLogoid'],
vendorDeliverypartner: json['vendorDeliverypartner'],
vendorTax: json['vendorTax'],
vendorPackingcharge: json['vendorPackingcharge'],
vendorCommission: json['vendorCommission'],
vendorBrkfaststart: json['vendorBrkfaststart'],
vendorBrkfastend: json['vendorBrkfastend'],
vendorLunchstart: json['vendorLunchstart'],
vendorLunchend: json['vendorLunchend'],
vendorSnacksstart: json['vendorSnacksstart'],
vendorSnacksend: json['vendorSnacksend'],
vendorDinnerstart: json['vendorDinnerstart'],
vendorDinnerend: json['vendorDinnerend'],
vendorFCM: json['vendorFCM'],
vendorStatus: json['vendorStatus'],
iimID: json['iimID'],
iimItemid: json['iimItemid'],
iimImageid: json['iimImageid'],
iimStatus: json['iimStatus'],
imageID: json['imageID'],
imageName: json['imageName'],
imageStatus: json['imageStatus'],
cuisineID: json['cuisineID'],
cuisineName: json['cuisineName'],
cuisineStatus: json['cuisineStatus'],
);
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['itemID'] = this.itemID;
data['itemVendorid'] = this.itemVendorid;
data['itemName'] = this.itemName;
data['itemCuisine'] = this.itemCuisine;
data['itemType'] = this.itemType;
data['itemCategory'] = this.itemCategory;
data['itemPrice'] = this.itemPrice;
data['itemDescription'] = this.itemDescription;
data['itemStatus'] = this.itemStatus;
data['LastAvailibityDate'] = this.lastAvailibityDate;
data['vendorID'] = this.vendorID;
data['vendorName'] = this.vendorName;
data['vendorE-Mail'] = this.vendorEMail;
data['vendorPassword'] = this.vendorPassword;
data['vendorPhone'] = this.vendorPhone;
data['vendorAddress'] = this.vendorAddress;
data['vendorLatitude'] = this.vendorLatitude;
data['vendorLongitude'] = this.vendorLongitude;
data['vendorGSTIN'] = this.vendorGSTIN;
data['vendorCuisine'] = this.vendorCuisine;
data['vendorLogoid'] = this.vendorLogoid;
data['vendorDeliverypartner'] = this.vendorDeliverypartner;
data['vendorTax'] = this.vendorTax;
data['vendorPackingcharge'] = this.vendorPackingcharge;
data['vendorCommission'] = this.vendorCommission;
data['vendorBrkfaststart'] = this.vendorBrkfaststart;
data['vendorBrkfastend'] = this.vendorBrkfastend;
data['vendorLunchstart'] = this.vendorLunchstart;
data['vendorLunchend'] = this.vendorLunchend;
data['vendorSnacksstart'] = this.vendorSnacksstart;
data['vendorSnacksend'] = this.vendorSnacksend;
data['vendorDinnerstart'] = this.vendorDinnerstart;
data['vendorDinnerend'] = this.vendorDinnerend;
data['vendorFCM'] = this.vendorFCM;
data['vendorStatus'] = this.vendorStatus;
data['iimID'] = this.iimID;
data['iimItemid'] = this.iimItemid;
data['iimImageid'] = this.iimImageid;
data['iimStatus'] = this.iimStatus;
data['imageID'] = this.imageID;
data['imageName'] = this.imageName;
data['imageStatus'] = this.imageStatus;
data['cuisineID'] = this.cuisineID;
data['cuisineName'] = this.cuisineName;
data['cuisineStatus'] = this.cuisineStatus;
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment