Skip to content

Instantly share code, notes, and snippets.

@mikekosulin
Created July 18, 2020 13:12
Show Gist options
  • Save mikekosulin/d6ff7ab0857e705b405944893655932a to your computer and use it in GitHub Desktop.
Save mikekosulin/d6ff7ab0857e705b405944893655932a to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class DrawerScreen extends StatefulWidget {
@override
_DrawerScreenState createState() => _DrawerScreenState();
}
List data;
class _DrawerScreenState extends State<DrawerScreen> {
@override
void initState() {
super.initState();
}
Future getData() async {
var response = await http.get(Uri.encodeFull("https://devuz.ru/api/1/catalog"), headers: {"Accept": "application/json"});
return json.decode(utf8.decode(response.bodyBytes));
}
@override
Widget build(BuildContext context) {
return Scaffold(body: _buildNewTiles());
}
FutureBuilder _buildNewTiles() {
return FutureBuilder(
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.none && snapshot.hasData == null) {
return Container(
child: Text('Nothing to show'),
);
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var element = snapshot.data[index];
return _buildTile(element);
},
);
},
future: getData(),
);
}
_buildTile(element) {
return ExpansionTile(title: Text(element['name']), children: _buildChildren(element['children']));
}
List<Widget> _buildChildren(List children) {
List<Widget> childCategories = [];
children.forEach((childCategory) {
childCategories.add(ListTile(
dense: true,
title: Text("@" + childCategory["name"]),
subtitle: Text(childCategory["name"]),
));
});
return childCategories;
}
}
@mikekosulin
Copy link
Author

проверку на null добавь и не будет валиться до загрузки

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment