无法从randomuser.me获取随机用户数据

时间:2020-05-01 16:45:49

标签: flutter flutter-web

我一直试图使用randomuser.me中的随机用户信息创建用于消息传递和发布的用户模型,但没有成功,没有索引数据在填充。我想念什么?任何帮助表示赞赏。 [![User Model [![User Posts Feed] [1]] [1]] [2]

//User Model

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class User {
final int index;
final String fname;
final String lname;
final String topic;
final String action;
final String timestamp;
final String location;
final String jobtitle;
final String usercompany;
final String posttext;
final String comments;
final String likes;
final int shares;
final String profilePic;
final String sayyicon;
final String gender;

User({
this.index,
this.fname,
this.lname,
this.topic,
this.action,
this.timestamp,
this.location,
this.jobtitle,
this.usercompany,
this.posttext,
this.comments,
this.likes,
this.shares,
this.profilePic,
this.sayyicon,
this.gender,
 });
 }

/// Random User Api Data

List<User> users = [];

Future<void> postsdata() async {
if (users.length == 0) {
var dataFromURL = await http.get("https://randomuser.me/api/? 
results=20");
var jsonData = json.decode(dataFromURL.body);
int index = 0;
for (var user in jsonData['results']) {
  User data = User(
    index: index,
    profilePic: user['picture']['thumbnail'],
    fname: user['name']['first'],
    lname: user['name']['last'],
    location: user['city']['state'],
    gender: user['gender'],

  );
  user.add(data);
  index++;
  }
  }
  }

 // ListView builder
import 
'package:Sayy_Web_App/Sayy_Data_Models/Sayy_User_Model/
Sayy_User_Models.dart';

导入“ package:flutter / material.dart”;

class SayyExamplePosts extends StatefulWidget {
@override
_SayyExamplePostsState createState() => _SayyExamplePostsState();
}

class _SayyExamplePostsState extends State<SayyExamplePosts> {
final scrollController = ScrollController();

@override
Widget build(BuildContext context) {
final data = MediaQuery.of(context);
return Stack(children: <Widget>[
  Container(
      height: data.size.height,
      width: data.size.width,
      child: ListView.builder(
          itemCount: users.length,
          itemBuilder: ((context, int index) =>
              Container(child: Text(users[index].location)))))
 ]);
 }
 }

0 个答案:

没有答案