0

I want to get data from json file but it returning error.I have been trying to fetch json data from my flutter app.Kindly tell me what i am doing wrong.The Error file is also included at the end.I want the json data in this format because i want to get data from wordpres rest api of page through id. Here is my code. main.dart

import 'dart:convert';

import 'package:flutter/material.dart';

main() => runApp(HomePage());

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

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder(
          future: DefaultAssetBundle.of(context).load("assets/file.json"),
          builder: (context, snapshot) {
            var mydata = json.decode(snapshot.data.toString());

            return Center(child: Text(mydata["name"]));
          },
        ),
      ),
    );
  }
}

file.json

{
    "name": "Shahryar"
}

Error

Restarted application in 2,001ms.
I/flutter (24449): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (24449): The following NoSuchMethodError was thrown building FutureBuilder<ByteData>(dirty, state:
I/flutter (24449): _FutureBuilderState<ByteData>#87cd8):
I/flutter (24449): The method '[]' was called on null.
I/flutter (24449): Receiver: null
I/flutter (24449): Tried calling: []("name")
I/flutter (24449):
I/flutter (24449): The relevant error-causing widget was:
[38;5;248mI/flutter (24449):   FutureBuilder<ByteData>[39;49m
I/flutter (24449):
I/flutter (24449): When the exception was thrown, this was the stack:
[38;5;244mI/flutter (24449): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)[39;49m
[38;5;248mI/flutter (24449): #1      _HomePageState.build.<anonymous closure>[39;49m
[38;5;244mI/flutter (24449): #2      _FutureBuilderState.build[39;49m
[38;5;244mI/flutter (24449): #3      StatefulElement.build[39;49m
[38;5;244mI/flutter (24449): #4      ComponentElement.performRebuild[39;49m
[38;5;244mI/flutter (24449): #5      StatefulElement.performRebuild[39;49m
[38;5;244mI/flutter (24449): #6      Element.rebuild[39;49m
[38;5;244mI/flutter (24449): #7      ComponentElement._firstBuild[39;49m
[38;5;244mI/flutter (24449): #8      StatefulElement._firstBuild[39;49m
[38;5;244mI/flutter (24449): #9      ComponentElement.mount[39;49m
I/flutter (24449): ...     Normal element mounting (19 frames)
[38;5;244mI/flutter (24449): #28     Element.inflateWidget[39;49m
[38;5;244mI/flutter (24449): #29     MultiChildRenderObjectElement.mount[39;49m
I/flutter (24449): ...     Normal element mounting (191 frames)
[38;5;244mI/flutter (24449): #220    Element.inflateWidget[39;49m
[38;5;244mI/flutter (24449): #221    MultiChildRenderObjectElement.mount[39;49m
I/flutter (24449): ...     Normal element mounting (255 frames)
[38;5;244mI/flutter (24449): #476    Element.inflateWidget[39;49m
[38;5;244mI/flutter (24449): #477    Element.updateChild[39;49m
[38;5;244mI/flutter (24449): #478    RenderObjectToWidgetElement._rebuild[39;49m
[38;5;244mI/flutter (24449): #479    RenderObjectToWidgetElement.mount[39;49m
[38;5;244mI/flutter (24449): #480    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>[39;49m
[38;5;244mI/flutter (24449): #481    BuildOwner.buildScope[39;49m
[38;5;244mI/flutter (24449): #482    RenderObjectToWidgetAdapter.attachToRenderTree[39;49m
[38;5;244mI/flutter (24449): #483    WidgetsBinding.attachRootWidget[39;49m
[38;5;244mI/flutter (24449): #484    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>[39;49m
I/flutter (24449): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter (24449):
I/flutter (24449): ════════════════════════════════════════════════════════════════════════════════════════════════════

[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following FormatException was thrown building FutureBuilder<ByteData>(dirty, state: _FutureBuilderState<ByteData>#87cd8):[39;49m
Unexpected character (at character 1)
TypedDataView(cid: 146)
^

[38;5;244mThe relevant error-causing widget was[39;49m
    [38;5;248mFutureBuilder<ByteData>[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      _ChunkedJsonParser.fail  (dart:convert-patch/convert_patch.dart:1394:5)[39;49m
[38;5;244m#1      _ChunkedJsonParser.parseNumber  (dart:convert-patch/convert_patch.dart:1261:9)[39;49m
[38;5;244m#2      _ChunkedJsonParser.parse  (dart:convert-patch/convert_patch.dart:926:22)[39;49m
[38;5;244m#3      _parseJson  (dart:convert-patch/convert_patch.dart:31:10)[39;49m
[38;5;244m#4      JsonDecoder.convert  (dart:convert/json.dart:495:36)[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=-1)
D/OpenSSLLib(24449): OpensslErr:Module:30(110:); file:external/boringssl/src/crypto/fipsmodule/cipher/cipher.c ;Line:563;Function:EVP_CIPHER_CTX_set_key_length
D/Surface (24449): Surface::connect(this=0x792c29f000,api=1)
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=-1)
D/Surface (24449): Surface::disconnect(this=0x792c29e000,api=1)
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
D/OpenSSLLib(24449): OpensslErr:Module:30(110:); file:external/boringssl/src/crypto/fipsmodule/cipher/cipher.c ;Line:563;Function:EVP_CIPHER_CTX_set_key_length
D/Surface (24449): Surface::connect(this=0x792c29e000,api=1)
D/OpenSSLLib(24449): OpensslErr:Module:30(110:); file:external/boringssl/src/crypto/fipsmodule/cipher/cipher.c ;Line:563;Function:EVP_CIPHER_CTX_set_key_length
D/Surface (24449): Surface::connect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=-1)
D/Surface (24449): Surface::disconnect(this=0x792c29e000,api=1)
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
D/OpenSSLLib(24449): OpensslErr:Module:30(110:); file:external/boringssl/src/crypto/fipsmodule/cipher/cipher.c ;Line:563;Function:EVP_CIPHER_CTX_set_key_length
D/Surface (24449): Surface::connect(this=0x792c29e000,api=1)
D/OpenSSLLib(24449): OpensslErr:Module:30(110:); file:external/boringssl/src/crypto/fipsmodule/cipher/cipher.c ;Line:563;Function:EVP_CIPHER_CTX_set_key_length
D/Surface (24449): Surface::connect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=1)
D/Surface (24449): Surface::disconnect(this=0x792c29f000,api=-1)
D/Surface (24449): Surface::disconnect(this=0x792c29e000,api=1)
V/PhoneWindow(24449): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@6820332, this = DecorView@d00c883[MainActivity]
Application finished.
Exited (sigterm)

2
  • Can you paste the error? Commented Apr 9, 2020 at 11:23
  • change the load() function to loadString() .. should work. Let me know if it worked. Commented Apr 9, 2020 at 13:54

1 Answer 1

2

You are reading a text file.

In this line

DefaultAssetBundle.of(context).load("assets/file.json"),

Change load for loadString

DefaultAssetBundle.of(context).loadString("assets/file.json"),

For reference:

load (String key) → Future< ByteData >

Retrieve a binary resource from the asset bundle as a data stream

-

loadString (String key, {bool cache: true}) → Future< String >

Retrieve a string from the asset bundle

https://api.flutter.dev/flutter/services/AssetBundle-class.html

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way of loading data from device e.g. File("/sdcard/Download/data.json")? Instead of from the flutter asset files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.