Last active
September 6, 2020 23:26
-
-
Save VB10/28873886fd67776beb494954b9db6e55 to your computer and use it in GitHub Desktop.
Json Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class BookJsonView extends StatefulWidget { | |
| @override | |
| _BookJsonViewState createState() => _BookJsonViewState(); | |
| } | |
| class _BookJsonViewState extends State<BookJsonView> { | |
| List<BookModel> books = [ | |
| BookModel("Genesis", Book(1, "24", [1, 1, 2, 3])) | |
| ]; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| // fill json use json to dart | |
| } | |
| List<int> getChapters(String name) { | |
| final model = books.firstWhere((item) => item.name == name); | |
| if (model != null) { | |
| print("Is okey"); | |
| return model.book.chapters; | |
| } | |
| return null; | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: ListView.builder( | |
| itemCount:books.length, | |
| itemBuilder: (context, index) { | |
| return ListTile( | |
| onTap: () { | |
| getChapters(books[index].name); | |
| }, | |
| title: Text( | |
| books[index].name, | |
| )); | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| class Book { | |
| final int id; | |
| List<int> chapters; | |
| final String description; | |
| Book(this.id, this.description, this.chapters); | |
| } | |
| class BookModel { | |
| String name; | |
| Book book; | |
| BookModel(this.name, this.book); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment