I want to parse JSON from the link in Flutter. Here is my api structure.
Here is the model.dart class which is generated using jsontodart.com.
Here is the services.dart class:
import 'dart:convert';
import 'package:get_api/model.dart';
import 'package:http/http.dart' as http;
class Services {
//
static const String url = 'http://...............';
static Future<List<User>> getUsers() async {
try {
final response = await http.get(url);
if (response.statusCode == 200) {
final body = jsonDecode(response.body);
//print(body);
final Iterable json = body;
return json.map((user) => User.fromJson(user)).toList();
} else {
return List<User>();
}
} catch (e) {
return List<User>();
}
}
}
Here is main.dart
import 'package:flutter/material.dart';
import 'package:get_api/model.dart';
import 'package:get_api/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: JsonParseDemo(),
);
}
}
class JsonParseDemo extends StatefulWidget {
//
JsonParseDemo() : super();
@override
_JsonParseDemoState createState() => _JsonParseDemoState();
}
class _JsonParseDemoState extends State<JsonParseDemo> {
//
List<User> _users;
bool _loading;
@override
void initState() {
super.initState();
_loading = true;
Services.getUsers().then((users) {
setState(() {
_users = users;
_loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_loading ? 'Loading...' : 'Users'),
),
body: Container(
color: Colors.white,
child: ListView.builder(
itemCount: _users == null ? 0 : _users.length,
itemBuilder: (context, index) {
User user = _users[index];
return ListTile(
title: Text(user.clientInfos[index].cLINTNAME),
subtitle: Text(user.clientInfos[index].cLINTEMAIL),
);
},
),
),
);
}
}
But the problem is that it only shows one list. How can I show all the list? I don't understand why it is not working.