I am trying to fetch data from realtime database using http mathod "GET". The data is being retreived but it's not showing up in listview and when i print the length of my list it's 0. And this is what the error is being shown in my terminal: Error: Expected a value of type 'int', but got one of type 'String'
I can't figure out what the problem is. Please help me solve this problem because i am trying for around 5 days but can't solve it.
Thank you.
These are my codes for fetching the data in lists.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(myapp());
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
/*Future<List> getData() async {
final response =
await http.get(Uri.https('dontworry.in', 'app/viewuser.php'));
var datareceived = jsonDecode(response.body);
//print(datareceived);
return datareceived;
}*/
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Future<List> getData() async {
//final response = await http.get(Uri.https('dontworry.in', 'app/viewuser.php'));
final response =
await http.get(Uri.parse('https://dontworry.in/app/viewuser.php'));
var datareceived = jsonDecode(response.body);
print(datareceived);
return datareceived;
}
@override
void initState() {
getData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Database"),
),
body: FutureBuilder(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasError) {
print('Error in loading' snapshot.hasError.toString());
}
return snapshot.hasData
? new Itemlist(
list: [snapshot.data],
)
: const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
class Itemlist extends StatelessWidget {
final List list;
const Itemlist({Key? key, required this.list}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: list == null ? 0 : list.length,
itemBuilder: (context, i) {
return Card(
child: ListTile(
title: new Text(list[i]['NAME']),
),
);
});
}
}
My PHP Code json encode is
<?php
include "config.php";
$sql= "SELECT * FROM contacts ORDER BY NAME";
$result= array();
$res=$con->query($sql);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
$result[]=$row;
}
}
echo json_encode($result);
?>
Please help me
CodePudding user response:
If you want a quick fix, and you are sure that the value is an int but in a string format, then you can use tryParse method
print(int.tryParse('2021')); // 2021
print(int.tryParse('1f')); // null
CodePudding user response:
I ran your code, your only issue is that you were wrapping your list into another list. Your code is this:
new Itemlist(list: [snapshot.data])
This means you are adding snapshot.data which is already a list, and you are adding it inside another list (by adding it inside brackets), so just do it like this:
new Itemlist(list: snapshot.data as List)
I see now your data coming through:
Suggestion:
Try creating a separate class that maps your data into a strongly typed model after fetching the JSON data, like in here https://docs.flutter.dev/cookbook/networking/fetch-data#create-an-album-class