This error comes when i tried to wrap the json object in Text widget,the code is as follows
import 'package:flutter/material.dart';
import '../drawer.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Homepage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var url = "http://jsonplaceholder.typicode.com/photos";
var data;
// Used to initialize something before starting of the screen
@override
void initState() {
// TODO: implement initState
super.initState();
fetchdata();
}
fetchdata() async {
var res = await http.get(url);
// print(res.body);
// json parsing
data = jsonDecode(res.body);
print(data);
//To tell the UI that we got the data
setState(() {});
}
@override
Widget build(BuildContext context) {
//Scaffold has prebuild some widget themes
return Scaffold(
backgroundColor: Colors.green[100],
appBar: AppBar(
title: Text("My App"),
),
// Container is similiar to <Div>
body: data != null
? ListView.builder(itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]["title"]),
);
})
As you can see ,I'm getting a HTTP response and converting it in to a json object and trying to display the title object in Listview on demand.Even though I wrapped inside the Text widget it show error as,
type 'String' is not a subtype of type 'Widget'
Please fix this error,THanks in advance~.Im new to flutter
You can copy paste run full code below
In check data != null
and return CircularProgressIndicator()
when data is null
code snippet
body: data != null
? ListView.builder(itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]["title"]),
);
})
: Center(child: CircularProgressIndicator()))
working demo
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Homepage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var url = "http://jsonplaceholder.typicode.com/photos";
var data;
// Used to initialize something before starting of the screen
@override
void initState() {
// TODO: implement initState
super.initState();
fetchdata();
}
fetchdata() async {
var res = await http.get(url);
// print(res.body);
// json parsing
data = jsonDecode(res.body);
print(data);
//To tell the UI that we got the data
setState(() {});
}
@override
Widget build(BuildContext context) {
//Scaffold has prebuild some widget themes
return Scaffold(
backgroundColor: Colors.green[100],
appBar: AppBar(
title: Text("My App"),
),
body: data != null
? ListView.builder(itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]["title"]),
);
})
: Center(child: CircularProgressIndicator()));
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Homepage(),
);
}
}