0

I'm trying to build a ListView.builder from a list like that:

 final List<myData> user1 = [
  user1(
          "name",
          "username",
         [user2.elementAt(0)]),

), 

And at first I didn't have any problem, the problem appeared when I created another list like this one:

 final List<otherData> user2 = [
  user2(
          "name",
          "username",
         [user1.elementAt(0)]),

), 

because I need to access to some information from both list, I need to use a list of both classes in both list (in user1 information of user2, and in user2 information about user1), however doesn't seems to be that Flutter like this kind of actions.

Anyway, after I did that, this error appeared. Any idea about where is the problem? here is my list builder:

 Expanded(
                child: ListView.builder(
                    padding: EdgeInsets.only(
                      top: 10.0,
                    ),
                    itemCount: user1.length,
                    itemBuilder: (context, index) {
                      final pokemon = user1[index];
                     

                      return GestureDetector(

I have also to say that I have another list view but with the other list as you can see bellow:

 Expanded(
                child: ListView.builder(
                    padding: EdgeInsets.only(
                      top: 10.0,
                    ),
                    itemCount: user2.length,
                    itemBuilder: (context, index) {
                      final pokemon = user2[index];
                     

                      return GestureDetector(

UPDATE:

I added a demo showing when is occurring this error, here is my code:

this is where I defined one of the two list:

import 'otherDara.dart';

class myData {
  String name;
  String username;
  List<otherData> user2;

  myData(
    this.name,
    this.username,
    this.user2
  );
}

final List<myData> user1 = [
  myData("JUAN", "PALOMO", [user2.elementAt(0)])
];

here is my second list

import 'package:temporal/myData.dart';

class otherData {
  String name;
  String username;
  List<myData> user1;

  otherData(this.name, this.username, this.user1);
}

final List<otherData> user2 = [
  //ESTO VA MUCHO MAS DESPUES
  otherData("MARCO", "POLO", [user1.elementAt(0)])
];

here is where I use the info of the list builded

// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:temporal/myData.dart';

class userDetail extends StatefulWidget {

  final myData user1;
  final List<myData> list;

  const userDetail({key, this.title, this.user1, this.list}) : super(key: key);

  final String title;

  @override
  State<userDetail> createState() => _userDetailtate();
}

class _userDetailtate extends State<userDetail> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView.builder(
            // ignore: prefer_const_constructors
            padding: EdgeInsets.only(
              top: 10.0,
            ),
            itemCount: widget.user1.username.length,
            itemBuilder: (context, index) {
              final userData = widget.user1.user2[index];
              return GestureDetector(
                child: Text(
                  userData.name, //NOOOOOOMBRE
                  style: TextStyle(
                    fontSize: 22.0,
                    color: Colors.black,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              );
            }));
  }
}

and finally, here is my main page:

import 'package:flutter/material.dart';

import 'myData.dart';
import 'userDetail.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<myData> list;
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 500,
      child: Container(
        //color: Colors.grey.shade300,
        child: Stack(children: [
          Column(
            children: <Widget>[
              // ignore: prefer_const_constructors
              SizedBox(
                height: 30,
              ),
              Expanded(
                child: ListView.builder(
                    // ignore: prefer_const_constructors
                    padding: EdgeInsets.only(
                      top: 10.0,
                    ),
                    itemCount: user1.length,
                    itemBuilder: (context, index) {
                      final userData = user1[index];

                      return GestureDetector(
                        onTap: () {
                          Navigator.push(context, 
                              MaterialPageRoute(builder: (context) {
                            return userDetail(user1: userData, list: user1);
                           
                          }));
                        },
                        child: Padding(
                          // ignore: prefer_const_constructors
                          padding: EdgeInsets.only(
                            bottom: 10.0,
                            right: 20.0,
                            left: 20.0,
                          ),
                          child: Container(),
                        ),
                      );
                    }),
              )
            ],
          ),
          //floatButton(),
        ]),
      ),
    );
  }
}

  • Is `user1` a method name or list variable name? – iDecode May 21 '22 at 15:06
  • is a list variable name – Juan Sin Miedo May 21 '22 at 15:09
  • How can you use a variable name before it's initialized? Your first code snippet is actually wrong. – iDecode May 21 '22 at 15:09
  • Are `user1` and `user2` member variables, global variables, or local variables? Your question title says `static`, but they don't seem to be declared as `static` variables. If they are member variables, see [Error: The instance member ... can't be accessed in an initializer](https://stackoverflow.com/q/65601214/). – jamesdlin May 21 '22 at 15:14
  • @iDecode As you can see in my update code, the variable is initialized at first. – Juan Sin Miedo May 21 '22 at 15:18
  • Post a minimum reproducible code. – iDecode May 21 '22 at 15:19
  • `final List user1 = [user1(...)]` is self-referential and is bogus code. Same thing for `user2`. – jamesdlin May 21 '22 at 16:23
  • @iDecode okey, I added a demo showing when is happening the error, thanks for your helping. – Juan Sin Miedo May 21 '22 at 16:25
  • @jamesdlin and how do I will do it? – Juan Sin Miedo May 21 '22 at 16:28
  • Your code is so confusing. First, you need to know that classes in Dart are named with Pascal case. Second, this is not a **minimal reproducible code**. – iDecode May 21 '22 at 17:41
  • @iDecode The problem here seems to be the following: There is a class (myData) with several attributes in which a list of type (otherData) is included. On the other hand, there is a class (otherData) with several attributes in which a list of type (myData) is included. That is, both contain each other, and so when initializing the first list or the second, I am not clear, the code can not access the values to be displayed. – Juan Sin Miedo May 21 '22 at 19:02

0 Answers0