Home > Software design >  Flutter http.get() giving null as data,
Flutter http.get() giving null as data,

Time:06-27

I am newbie in coding & started by learning flutter, so i am struggling to execute the this in dart,somebody please explain

i want return email id in widget,but when i print the output its giving me null as output,i cant find what's the error,please tell why it is returning null

Modelclass.dart

  int? page;
  int? perPage;
  int? total;
  int? totalPages;
  List<Data>? data;
  Support? support;

  Modelclass(
      {this.page,
      this.perPage,
      this.total,
      this.totalPages,
      this.data,
      this.support});

  Modelclass.fromJson(Map<String, dynamic> json) {
    page = json['page'];
    perPage = json['per_page'];
    total = json['total'];
    totalPages = json['total_pages'];
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    support =
        json['support'] != null ? new Support.fromJson(json['support']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['page'] = this.page;
    data['per_page'] = this.perPage;
    data['total'] = this.total;
    data['total_pages'] = this.totalPages;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    if (this.support != null) {
      data['support'] = this.support!.toJson();
    }
    return data;
  }
}

class Data {
  int? id;
  String? email;
  String? firstName;
  String? lastName;
  String? avatar;

  Data({this.id, this.email, this.firstName, this.lastName, this.avatar});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    email = json['email'];
    firstName = json['first_name'];
    lastName = json['last_name'];
    avatar = json['avatar'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['email'] = this.email;
    data['first_name'] = this.firstName;
    data['last_name'] = this.lastName;
    data['avatar'] = this.avatar;
    return data;
  }
}

class Support {
  String? url;
  String? text;

  Support({this.url, this.text});

  Support.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    text = json['text'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['url'] = this.url;
    data['text'] = this.text;
    return data;
  }
}

Repository.dart


import 'package:http/http.dart' as http;
import 'package:neww/Model/Modelclass.dart';
import 'package:neww/Model/model2.dart';

const url = 'https://reqres.in/api/users/';

class Repository {
  Future<Data?> getdata() async {
    final result = await http.get(Uri.parse(url));
    if (result.statusCode != 200) {
      return null;
    }
    final decodeddata = jsonDecode(result.body);

    final neww = Data.fromJson(decodeddata);
    print(neww.email);

    return neww;
  }
}

CodePudding user response:

Here is what to do:

import 'package:http/http.dart' as http;
import 'package:neww/Model/Modelclass.dart';
import 'package:neww/Model/model2.dart';

const url = 'https://reqres.in/api/users/';

class Repository {
  Future<Data?> getdata() async {
    final result = await http.get(Uri.parse(url));
final decodeddata = jsonDecode(result.body);
    if (result.statusCode == 200) { //the statement reads like if result found is ok

    final neww = Data.fromJson(decodeddata);
    print(neww['data']['email']); //access the email by getting in to data


    return neww;//return something of type `Data?` instead of null
     
    }else{ //else not ok `!= 200`
return Data.fromJson(decodeddata);
    
  }
}

CodePudding user response:

There is mistake here : use Modelclass instead of Data while parse as it inside that.

import 'package:http/http.dart' as http;
import 'package:neww/Model/Modelclass.dart';
import 'package:neww/Model/model2.dart';

const url = 'https://reqres.in/api/users/';

class Repository {
  Future<Modelclass?> getdata() async {
    final result = await http.get(Uri.parse(url));
    if (result.statusCode != 200) {
      return null;
    }
    final decodeddata = jsonDecode(result.body);

    final neww = Modelclass.fromJson(decodeddata);
    // print(neww.email);

    return neww;
  }
}

And on the other side while calling :

  var d = await getdata();
  
  print(d!.data![0].email);

this way you can access.

  • Related