Reputation: 676
Cant parse JSON data from a URL, getting error "_TypeError (type '_InternalLinkedHashMap' is not a subtype of type 'FutureOr>')". So You can check the datas from the link. and let me know what I am doing wrong, Thanks in advance.
I tried using map, not worked.
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
primarySwatch: Colors.blue,
),
home: ui_hotel_list(),
);
}
}
class ui_hotel_list extends StatefulWidget {
@override
_ui_hotel_listState createState() => _ui_hotel_listState();
}
class _ui_hotel_listState extends State<ui_hotel_list> {
List hotels;
Future<List> _getHotels() async {
String serviceURL="**************************************************************************************";
var response = await http.get(serviceURL);
return json.decode(response.body.toString());
}
printv() async {
hotels= await _getHotels();
for(var i=0;i<hotels.length;i++) {
print(hotels[i]["hotels"]["name"]);
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
this._getHotels();
this.printv();
}
@override
Widget build(BuildContext context) {
return Container(
child: ListView.builder(
padding: EdgeInsets.all(10),
itemCount: hotels.length==0?0:hotels.length,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: Text(hotels[index]["hotels"]["name"]),
subtitle:Text(hotels[index]["hotels"]["location"]),
);
},
),
);
}
}
I want to get the data from that URL and show in my listview. What i need to add, please suggest me, thanks.
Upvotes: 0
Views: 112
Reputation: 54367
You need dynamic and access like this
You can paste and run full code below
code snippet
for (var i = 0; i < responseData["hotels"].length; i++) {
print(responseData["hotels"][i]["name"]);
}
...
ListTile(
title: Text(responseData["hotels"][index]["name"]),
subtitle: Text(responseData["hotels"][index]["location"]),
),
full code
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ui_hotel_list(),
);
}
}
class ui_hotel_list extends StatefulWidget {
@override
_ui_hotel_listState createState() => _ui_hotel_listState();
}
class _ui_hotel_listState extends State<ui_hotel_list> {
dynamic responseData;
bool loading = false;
Future<dynamic> _getHotels() async {
setState(() {
loading = true;
});
String serviceURL =
"https://baseURL/api/v2/search/filtered/?checkin=08-11-2019&checkout=09-11-2019&adults=2&rooms=1&location=1&page=1";
var response = await http.get(serviceURL);
return json.decode(response.body.toString());
}
printv() async {
responseData = await _getHotels();
setState(() {
loading = false;
});
for (var i = 0; i < responseData["hotels"].length; i++) {
print(responseData["hotels"][i]["name"]);
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
this._getHotels();
this.printv();
}
@override
Widget build(BuildContext context) {
return loading
? Container(height: 100, width: 100, child: CircularProgressIndicator())
: Container(
child: ListView.builder(
padding: EdgeInsets.all(10),
itemCount: responseData["hotels"].length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: ListTile(
title: Text(responseData["hotels"][index]["name"]),
subtitle: Text(responseData["hotels"][index]["location"]),
),
);
},
),
);
}
}
Upvotes: 1
Reputation: 2717
It seems to me that the problem comes from the _getHotels()
method. It says it returns a Future<List>
, because that is what you expect. But Flutter has no way to know what the URL you provided holds (https://amarroom...
). It turns out that when you post an HTTP request with that URL, it returns:
{
"special_amenities": [
1,
5,
11,
48,
45
],
"facets": {
"neighborhood": [
[
...
}
which is not a List
, it is a JSON, that is, a Map<String, dynamic>
in Dart. So your _getHotels()
function should not return a Future<List>
, it should return a Future<Map<String, dynamic>>
.
Then you can do whatever you want to your Map<String, dynamic>
returning from that function and turn it into a List
, if that is what you need.
Upvotes: 0