नमस्ते, मैं इस यूआरएल 'https://jsonplaceholder.typicode.com/todos/1' से एक JSON अनुरोध करने की कोशिश कर रहा हूं और एक सूची दृश्य निर्माता पर डेटा दिखा रहा हूं लेकिन जब मैं कुछ भी नहीं चलाता हूं और ऐप को छोड़कर स्क्रीन खाली है बार

यह मुख्य वर्ग है जो अनुरोध करता है और सूची में डेटा दिखाता है

import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import 'data/note.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page3'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Note> _notes = List<Note>();

  Future<List<Note>> fetchJson() async {
    var url = ('https://jsonplaceholder.typicode.com/todos/1');
    var response = await http.get(url);

    var notes = List<Note>();

    if (response.statusCode == 200) {
      var jsons = json.decode(response.body);
      for (var jsons2 in jsons) {

        notes.add(Note.fromJson(jsons2));
      }
      return notes;
    } else {
      print('Failed to fetch data');
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    fetchJson().then((value) {
      setState(() {
        _notes.addAll(value);
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text(widget.title),
        ),
        body: ListView.builder(
          itemBuilder: (context, index) {
            return Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      _notes[index].text,
                      style: TextStyle(fontSize: 20),
                    ),
                    Text(
                     _notes[index].title,
                      style: TextStyle(fontSize: 15, color: Colors.grey),
                    ),
                  ],
                ),
              ),
            );
          },
          itemCount: _notes.length,
        ));
  }
}

यहाँ दूसरा वर्ग है जिसमें दो कंस्ट्रक्टर हैं

class Note{

  String title;
  String text;

  Note(this.text,this.title);

  Note.fromJson(Map<String, dynamic>json){

    title = json['title'];
    text = json['text'];

  }

}

जब मैं ऐप चलाता हूं तो यह त्रुटि दिखाई देती है

E/flutter (13048): #0      _MyHomePageState.fetchJson (package:learn_json_phase2/main.dart:63:26)
E/flutter (13048): <asynchronous suspension>
E/flutter (13048): #1      _MyHomePageState.initState (package:learn_json_phase2/main.dart:76:5)
E/flutter (13048): #2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter (13048): #3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter (13048): #4      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #5      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #6      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter (13048): #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter (13048): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter (13048): #11     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter (13048): #12     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter (13048): #13     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #14     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #15     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter (13048): #16     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #17     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #18     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter (13048): #19     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #20     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #21     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter (13048): #22     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter (13048): #23     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter (13048): #24     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter (13048): #25     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter (13048): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #28     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter (13048): #29     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #31     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter (13048): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter (13048): #35     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter (13048): #36     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter (13048): #37     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter (13048): #38     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter (13048): #39     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter (13048): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter (13048): #41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter (13048): #42     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter (13048): #43     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter (13048): #44     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter (13048): #45     Element.inflateWidget (packag
0
Farooq Txr 18 अप्रैल 2021, 16:02

2 जवाब

सबसे बढ़िया उत्तर

ऐसा लगता है कि प्रतिक्रिया डेटा सूची नहीं है।

इस लाने का प्रयास करें

 Future<List<Note>> fetchJson() async {
    var url = ('https://jsonplaceholder.typicode.com/todos'); //Change
    var response = await http.get(url);

    var notes = List<Note>();

    if (response.statusCode == 200) {
      var jsons = json.decode(response.body);
      for (var jsons2 in jsons) {

        notes.add(Note.fromJson(jsons2));
      }
      return notes;
    } else {
      print('Failed to fetch data');
    }
  }

0
Sanjeev Madhav 18 अप्रैल 2021, 13:13

https://jsonplaceholder.typicode.com/todos/1 जोंस मैप लौटाएं नहीं सूची

यहाँ आप मानचित्र पर लूप कर रहे हैं जो सही नहीं है

    for (var jsons2 in jsons) {
        notes.add(Note.fromJson(jsons2));
      }

Url को https://jsonplaceholder.typicode.com/todos में बदलने का प्रयास करें जो todos की सूची लौटाएगा

तो आपका कोड इस तरह बन जाएगा

  var url = ('https://jsonplaceholder.typicode.com/todos/1');
  
1
Moaid ALRazhy 18 अप्रैल 2021, 13:22