Flutter 'List<Post>' has no instance getter 'lenght'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maritza
    New Member
    • Sep 2021
    • 2

    Flutter 'List<Post>' has no instance getter 'lenght'

    I'm new to flutter. I am getting an error like this, can you help me?
    I've been stuck in http for json for 5 days, the codes in the source don't work. :( L

    It says list not entered but when I enter it does not accept it. I don't know on which line the problem is, but I got a warning like. "The following NoSuchMethodErr or was thrown building FutureBuilder(d irty, state: _FutureBuilderS tate#447cc):"

    https://dosya.co/w8rxecrl44rz/Capture.JPG.htm l
    https://dosya.co/vk6rd85o7zkx/Capture.JPG2.JP G.html



    Code:
    import 'dart:convert';
    
    Post postFromJson(String str) => Post.fromJson(json.decode(str));
    
    String postToJson(Post data) => json.encode(data.toJson());
    
    class Post {
      Post({
        required this.userId,
        required this.id,
        required this.title,
        required this.body,
      });
    
      int userId;
      int id;
      String title;
      String body;
    
      factory Post.fromJson(Map<String, dynamic> json) => Post(
        userId: json["userId"],
        id: json["id"],
        title: json["title"],
        body: json["body"],
      );
    
      Map<String, dynamic> toJson() => {
        "userId": userId,
        "id": id,
        "title": title,
        "body": body,
      };
    }
    Code:
    import 'dart:convert';
    
    
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'package:json_http_place_holder/post.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      Future getData = Future(() => null);
      var con = Uri.parse("https://jsonplaceholder.typicode.com/posts");
    
      Future<List<Post>> fetchPost() async {
        List<Post> result = <Post>[];
        final response = await http.get(con);
        if (response.statusCode == 200) {
          List listPost = jsonDecode(response.body);
          for (int i = 0; i < listPost.length; i++) {
            Post item = Post.fromJson(listPost[i]);
            result.add(item);
          }
        }
        return result;
      }
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        getData = fetchPost();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter HTTP json',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            body: FutureBuilder(
              future: getData,
              builder: (BuildContext context,  AsyncSnapshot<dynamic> snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                } else if (snapshot.connectionState == ConnectionState.none) {
                  return const Center(
                    child: Text("Bir hata meydana geldi"),
                  );
                } else if (snapshot.connectionState == ConnectionState.done) {
                  return ListView.separated(itemBuilder: (context,index){
                    return ListTile(
                      leading: Icon(Icons.local_post_office),
                      title: Text(snapshot.data[index].title),
                      subtitle: Text(snapshot.data[index].body),
                    );
                  },separatorBuilder: (context,index)=>Divider(),
                    itemCount: snapshot.data.lenght,
                  );
                }
                //var d =jsonDecode(snapshot.data.body);
                return Container();
              },
            ),
          ),
        );
      }
    }
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Flutter 'List<Post>' has no instance getter 'lenght'
    Did you try length?‎‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

    Comment

    Working...