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

Time:09-17

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 NoSuchMethodError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#447cc):"

enter image description here

enter image description here

// To parse this JSON data, do
//
//     final post = postFromJson(jsonString);

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,
  };
}
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();
          },
        ),
      ),
    );
  }
}

CodePudding user response:

You did not spell length well.

The error tells No such method on list lenght

The correct spelling for getting the method is .length

Change

 itemCount: snapshot.data.lenght,

to

 itemCount: snapshot.data.length,

CodePudding user response:

In your main.dart, FutureBuilder => separatorBuilder =>

itemCount: snapshot.data.lenght,

Change the itemCount from that to :

itemCount: snapshot.data.length,

  • Related