Saya sedang membuat aplikasi menggunakan petunjuk ini (https://www.developerlibs.com/2018/11/flutter-how-to-parse-local-json-file-in.html) tentang cara mengurai file JSON lokal, tetapi Saya menemukan pesan kesalahan. Saya masih pemula di Flutter, jadi bantuan apa pun sangat dihargai:

l/flutter ( 8377): The following NoSuchMethodError was thrown building FutureBuilder<String>(dirty, state: FutureBuilderState<String>#8f2a5):
l/flutter ( 8377): The method 'cast' was called on null.
l/flutter ( 8377): Receiver: null
l/flutter ( 8377): Tried calling: cast<Map<String, dynamic>>().

Kode:

main.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_load_local_json/country.dart';
import 'package:flutter_load_local_json/list.dart';

void main() {
  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    theme: new ThemeData(
      primaryColor: const Color(0xFF02BB9F),
      primaryColorDark: const Color(0xFF167F67),
      accentColor: const Color(0xFF167F67),
    ),
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  List data;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Load local JSON file",
          style: new TextStyle(color: Colors.white),),
        ),
        body: new Container(
          child: new Center(
            // Use future builder and DefaultAssetBundle to load the local JSON file
            child: new FutureBuilder(
                future: DefaultAssetBundle.of(context)
                    .loadString('assets/country.json'),
                builder: (context, snapshot) {
                  List<Country> countries =
                      parseJosn(snapshot.data.toString());
                  return !countries.isEmpty
                      ? new CountyList(country: countries)
                      : new Center(child: new CircularProgressIndicator());
                }),
          ),
        ));
  }

  List<Country> parseJosn(String response) {
    if(response==null){
      return [];
    }
    final parsed =
        json.decode(response.toString()).cast<Map<String, dynamic>>();
    return parsed.map<Country>((json) => new Country.fromJson(json)).toList();
  }
}

Terima kasih sebelumnya

0
Marhis 11 April 2020, 17:51

1 menjawab

Jawaban Terbaik

Anda harus menunggu masa depan selesai sebelum mem-parsing JSON :

FutureBuilder(
  future: DefaultAssetBundle.of(context).loadString('assets/country.json'),                    
  builder: (context, snapshot) {
    if (snapshot.connectionState != ConnectionState.done) {
      return Text("Loading JSON...")
    }

    List<Country> countries = parseJson(snapshot.data.toString());
    return countries.isNotEmpty 
      ? new CountyList(country: countries)
      : new Center(child: Text("Empty list");
  }
),

Info lebih lanjut: https://api.flutter.dev/flutter/widgets/FutureBuilder -class.html dan https://flutter.dev/docs/development/data-and-backend /json

1
woprandi 11 April 2020, 16:17