skip to Main Content

i am having problems not being able to access the attributes of an object in dart.

I am testing stuff for the database with GetIt, and i currently only have local data stored in a different file in the same program.

So when I try to gather the object data from a file, to use it in a different file where I wilp create the widget,,, the data of the object cannot be accessed.

Cannot even print them in the debug console, I tried print(Student.id) and it returns nothint.

I also tried not using GetIt, just a simple file with a instance of my object created, and it does the same thing.

Is not returning null, it prints

Instance of Object$

Not sure what is going

This is from a file



studentProfilecreation tempo =
    studentProfilecreation("Firebaseid1", "A", "B", '', "tdd");

This is from a different file, where I want to use the attributes for the widget.

 Widget build(BuildContext context) {
    //studentProfilecreation temp =
    //(locator.get<localData>().collectionOfstudents[0]);
    //print(temp);

    studentProfilecreation temp = tempo;
    String id = tempo.id;
    print(id);

    MediaQueryData queryData;

It doesnt print anything, but if type print(temp), it prints instance of ObjectX created$

This is object I am trying to get values from

class studentProfilecreation {
  String id = "";
  String firstName = "";
  String lastName = "";
  String profileImage = '';
  String major = "";
  String bio = "";
  Map<String, File> coursesAndnotes = Map();
//Constructor
  studentProfilecreation(
    String id,
    String firstName,
    String lastName,
    String profileImage,
    String major,
  ) {
    id = id;
    firstName = firstName;
    lastName = lastName;
    profileImage = profileImage;
    major = major;
  }

  studentProfilecreation.studentProfilecreationempty() {
    id = "";
    firstName = "";
    lastName = "";
    profileImage = '';
    major = "";
  }

  studentProfilecreation.createcopy(studentProfilecreation original) {
    id = original.id;
    firstName = original.firstName;
    lastName = original.lastName;
    profileImage = original.profileImage;
    major = original.major;
  }

And this is where I Want to the data to create the widget, the rest is just widget stuff

class studentProfile extends StatelessWidget {
  String id = "";
  //parameter value will come from tap of student profile from list of viewable students.
  studentProfile(String id) {
    this.id = id;
  }

  @override
  Widget build(BuildContext context) {
    //studentProfilecreation temp =
    //(locator.get<localData>().collectionOfstudents[0]);
    //print(temp);

    studentProfilecreation temp = tempo;
    String id = tempo.id;
    print(id);

2

Answers


  1. Without code snippet, I can only rule out that the most probable cause is the object retrieved from the local data store has not been defined as instance of Student class – hence the print out says Instance of Object$

    had it been defined as as instance of Student class, the printout would have said instance of Student instead.

    If that’s the culprit, the following steps should give the solution, using flutter as my framework.

    First we define Student class, by creating a model folder and then create file – lets call it student_model.dart

    class Student {
      Student({required this.id, required this.name, required this.course});
    
      final String id;
      final String name;
      final String course;
    }
    

    then, let create another folder called database and inside it, a file – let’s call it local_data.dart, where we will create a student object – let’s call it firstStudent, using Student class we declared earlier in student_model.dart file (don’t forget to import this file)

    import 'package:[*your_project_name*]/model/student.dart';
    
    class LocalData {
      static Student firstStudent =
          Student(id: '123', name: 'John Citizen', course: 'Computer Science');
    }
    

    finally in any of your file, you can call print as follow

    import 'package:flutter/material.dart';
    import 'package:[*your_project_name*]/database/local_data.dart';
    
    class MyWidget extends StatelessWidget {
      const MyWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        final myStudent = LocalData.firstStudent;
        print(myStudent.id);
        return const Placeholder();
      }
    }
    
    Login or Signup to reply.
  2. Change your constructor like:

    studentProfilecreation(
        this.id,
        this.firstName,
        this.lastName,
        this.profileImage,
        this.major,
      );
    

    Now you just overwrite its parameters by themselves without assigning to the fields

    UPDATE:

    class Foo {
      int i = 0;
    
      Foo(int i) {
        i = i;
      }
    }
    

    When Dart lexical analyzer is searching for the variable, it looks "from down to up" and stops when first suitable var is found. In my example this is a constructor parameter, not class field (because parameter’s definition is closer to the assignment line then field). So if you want to make assignment to the field, you should use

    this.i = i;

    and in this case you’ll get a hint

    Use an initializing formal to assign a parameter to a field. Try using
    an initialing formal (‘this.i’) to initialize the field.

    It will be good to have a warning message about assigning variable to itself too. Maybe, in the next Dart version we’ll see it 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search