PoojaB26/ParsingJSON-Flutter

hello can you help?

atta1234 opened this issue · 0 comments

i have create this class from the same modal you provided with diffrent data, but having issues

import 'dart:io';
import 'package:flutter/material.dart';
import 'product/products.dart';
import 'package:provider/provider.dart';
import 'utils/auth.dart';
import 'navigation.dart';
import 'package:mosquegiving/utils/api_util.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'product/productdetail.dart';
import 'subscription/plan.dart';
import 'searchresult.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';

void main() {
runApp(ChangeNotifierProvider(
create: (BuildContext context) => AuthProvider(),
child: MyApp(),
));
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'MY Finance',
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@OverRide
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
var _url = Uri.parse(ApiUtility.Main_Url + '/products');
var nexturl;
double average = 2;
ScrollController _scrollController = new ScrollController();
//determine if all data has been recieved
var loadCompleted = false;
List tempList = [];

var mycontroler = TextEditingController();
@OverRide
void initState() {
super.initState();
readToken();
data = getData(_url);
scrollindecator();
}

late Future<List> data;

void scrollindecator() {
_scrollController.addListener(
() {
if (_scrollController.offset >=
_scrollController.position.maxScrollExtent &&
!_scrollController.position.outOfRange) {
print('reach to bottom botton');
if (!loadCompleted) {
setState(() {
data = getData(Uri.parse(nexturl));
});
}
}
},
);
}

Future<List> getData(_url) async {
final response = await http.get(_url, headers: {});
if (response.statusCode == 200) {
// ignore: unused_local_variable
var data3 = json.decode(response.body)['links'];
if (data3['next'] != null) {
nexturl = data3['next'];
} else {
loadCompleted = true;
}
var data =
List<Map<String, dynamic>>.from(json.decode(response.body)['data']);
for (int i = 0; i < data.length; i++) {
tempList.add(Product.fromJson(data[i]));
}
return tempList;
} else {
throw SocketException('Make sure you have an internet connection');
}
}

void dispose() {
super.dispose();
_scrollController.dispose();
mycontroler.dispose();
}

void readToken() async {
await Provider.of(context, listen: false).tryToken();
}

Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
double text = MediaQuery.textScaleFactorOf(context);
return Scaffold(
appBar: AppBar(
title: Text(
'Mosque Giving',
style: TextStyle(color: Color(0XFF1A1A1A)),
),
centerTitle: true,
elevation: 0,
backgroundColor: Color(0xFF50BC90),
),
body: ListView(
controller: _scrollController,
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.max,
children: [
Text(
'Giving to your local mosque made easy!',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: text * 16,
),
),
SizedBox(
height: height * 0.03,
),
Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(0.0),
color: Colors.grey[200],
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 1.0),
blurRadius: 1.0,
),
],
),
child: TextField(
controller: mycontroler,
decoration: InputDecoration(
icon: Padding(
padding: EdgeInsets.only(left: 12.0),
child: Icon(
Icons.search,
color: Colors.grey,
),
),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
),
),
),
),
SizedBox(width: width * 0.00),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(0.0),
color: Colors.grey[200],
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 1.0),
blurRadius: 1.0,
),
],
),
child: SizedBox(
height: height * 0.06,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
),
primary: Color(0xFF50BC90)),
onPressed: () {
if (mycontroler.text == '') {
final snackBar = SnackBar(
content: Text('Please enter a keyword!'));
ScaffoldMessenger.of(context)
.showSnackBar(snackBar);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Search(mycontroler.text)),
);
}
},
child: Text('search')),
),
),
],
),
SizedBox(
height: 10.0,
),
Row(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'All Mosque',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
],
),
SizedBox(
height: 10.0,
),
],
),
],
),
),
FutureBuilder<List>(
future: data,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
physics: ScrollPhysics(),
shrinkWrap: true,
padding: const EdgeInsets.all(8),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
Product product = snapshot.data![index];
if (index == snapshot.data!.length - 1 &&
!loadCompleted) {
return Center(
child: new Opacity(
opacity: 1.0,
child: new CircularProgressIndicator(),
),
);
} else {
return Padding(
padding: const EdgeInsets.only(bottom: 15),
child: InkWell(
child: Card(
elevation: 0,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.grey[200],
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 1.0),
blurRadius: 1.0,
),
],
),
// color: Colors.black12,
padding: EdgeInsets.all(2),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: [
Container(
width: double.infinity,
child: Stack(
children: [
Container(
padding: EdgeInsets.only(top: 1),
width: double.infinity,
// child: Image(
// image: NetworkImage(
// ApiUtility.Assest_Url +
// product.dealimage),
// height: 180,
// fit: BoxFit.fitWidth),
),
],
),
),
Container(
width: double.infinity,
child: Stack(
alignment: Alignment.bottomLeft,
children: [
Container(
// color: Colors.black.withOpacity(0.5),
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: [
Padding(
padding:
const EdgeInsets.all(
1.0),
child: Text(
product.images[0].title,
style: TextStyle(
fontSize: 15,
fontWeight:
FontWeight.bold,
),
)),
Padding(
padding:
const EdgeInsets.fromLTRB(
0, 8, 0, 8),
child: RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Icon(
Icons.location_on,
size: 18,
color: Color(
0xFF50BC90)),
),
TextSpan(
text:
product.address,
style: TextStyle(
color: Colors
.black)),
],
),
),
),
Row(
children: [
RatingBar.builder(
initialRating: average,
minRating: 0,
direction:
Axis.horizontal,
allowHalfRating: false,
itemCount: 5,
itemPadding:
EdgeInsets.symmetric(
horizontal: 4.0),
itemBuilder:
(context, _) => Icon(
Icons.star,
color:
Color(0xFF50BC90),
),
onRatingUpdate: (rating) {
print(rating);
},
),
],
),
Row(
children: [
Container(
height: 40,
child: ElevatedButton(
style: ElevatedButton
.styleFrom(
primary: Color(
0xFF50BC90)),
onPressed: () {
Navigator.of(context)
.push(
_createRoute(
product
.id));
},
child:
Text('Donate Now'),
),
),
SizedBox(width: 3),
Container(
height: 40,
child: ElevatedButton(
style: ElevatedButton
.styleFrom(
primary: Color(
0xFF50BC90)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) =>
new Productdetails(
prd:
new Product(
id: product
.id,
address:
product.address,
title: product
.title,
category:
product.category,
dealimage:
product.dealimage,
description:
product.description,
slug: product
.slug,
))));
},
child: Text(
'View Details'),
),
),
],
)
],
),
),
],
),
),
],
),
),
),
),
);
}
},
);
} else if (snapshot.hasError) {
return Center(
child: Text(
"Make sure you have an active internet connection"));
}
return Center(child: CircularProgressIndicator());
}),
],
// This trailing comma makes auto-formatting nicer for build methods.
),
drawer: Navigation(),
);
}

Route _createRoute(id) {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => Plan(id),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return child;
},
);
}
}