Tril1lion
Tril1lion

Reputation: 11

Format Exception, FutureBuilder dirty? 'Unexpected end of input (at character 1)'?

First time posting, I'm very new to coding, and I am trying to learn about loading local json into flutter. I followed this tutorial https://www.youtube.com/watch?v=bTwTKwK3hGc to the letter and triple checked and cannot find any differences, and no errors show in the editor, but when I try to run the code I get a "FormatException" error.

The code:

import 'package:flutter/material.dart';
import 'dart:convert';

void main() => runApp(new MaterialApp(
  theme: new ThemeData(
    primarySwatch: Colors.teal,
  ),
  home: new HomePage(),
));

class HomePage extends StatefulWidget {
  @override 
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {
  List data;
  @override 
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Load Json Practice"),
      ),
      body: new Container(
        child: new Center(
          child: new FutureBuilder(
            future: DefaultAssetBundle
              .of(context)
              .loadString('load_json/stuff.json'),
          builder: (context, snapshot) {
            //decode json:
            var mydata = jsonDecode(snapshot.data.toString());

            return new ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return new Card(
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      new Text("Name: " + mydata[index]['name']),
                    ],
                  ),
                );
              },
              itemCount: mydata == null ? 0 : mydata.length,
            );
          },    
          ),
        ),
      ),
    );
    }
}

My Json file:

[{
    "id":1,
    "name":"example"
}
]

My error:

════════ Exception caught by widgets library ═══════════════════════════════════
The following FormatException was thrown building FutureBuilder<String>(dirty, state: _FutureBuilderState<String>#07cf5):
Unexpected end of input (at character 1)

^

User-created ancestor of the error-causing widget was
    Center 
lib/main.dart:25
When the exception was thrown, this was the stack
#0      _ChunkedJsonParser.fail  (dart:convert-patch/convert_patch.dart:1392:5)
#1      _ChunkedJsonParser.close  (dart:convert-patch/convert_patch.dart:510:7)
#2      _parseJson  (dart:convert-patch/convert_patch.dart:30:10)
#3      JsonDecoder.convert  (dart:convert/json.dart:493:36)
#4      JsonCodec.decode  (dart:convert/json.dart:151:41)
...

I am still very bad at understanding errors I get, I have googled everything I can think of to try and solve this on my own and have no idea how to troubleshoot this problem further. Any help or suggestions would be appreciated. Thank you for your time.

Upvotes: 1

Views: 632

Answers (1)

chunhunghan
chunhunghan

Reputation: 54397

I have test your code and your code is correct
Step 1 : please check json file directory, you can see picture 1
Step 2 : check pubspec.yaml setting (picture 2)
In pubspec.yaml, space and indent are important
Step 3 : check you json data, does it contains abnormal character (picture 3)

assets:
  - load_json/

enter image description here

enter image description here

enter image description here

Upvotes: 1

Related Questions