我正在使用 json.decode 将我的 JSON 数据转换为 User 类型的对象。我在 FutureBuilder 小部件中使用以下 Future。这是代码。
Future<List<User>> _getData() async {
var data = await http.get("http://www.json-generator.com/api/json/get/cvAgrXxhOW?indent=2");
var jsonBody = json.decode(data.body);
List<User> users = [];
for (var user in jsonBody) {
print(user);
User u = new User(user);
users.add(u);
}
print(users.length);
return users;
}
类 User 如下所示。
class User {
String _id;
int index;
String about;
String name;
String picture;
String gender;
int age;
String registered;
double longitude;
String email;
String eyeColor;
String phone;
String address;
double latitude;
String balance;
String guid;
String company;
bool isActive;
User(dynamic data){
this._id = json.decode(data)["_id"];
this.index = int.parse(json.decode(data)["index"]);
this.about = json.decode(data)["about"];
this.name = json.decode(data)["name"];
this.picture = json.decode(data)["picture"];
this.gender = json.decode(data)["gender"];
this.age = int.parse(json.decode(data)["age"]);
this.registered = json.decode(data)["registered"];
this.longitude = double.parse(json.decode(data)["longitude"]);
this.email = json.decode(data)["email"];
this.eyeColor = json.decode(data)["eyeColor"];
this.phone = json.decode(data)["phone"];
this.address = json.decode(data)["address"];
this.latitude = double.parse(json.decode(data)["latitude"]);
this.balance = json.decode(data)["balance"];
this.guid = json.decode(data)["guid"];
this.company = json.decode(data)["company"];
this.isActive = json.decode(data)["isActive"];
}
getUser(){
return this;
}
}
User u = new User(user); 行不会让代码进一步执行。 _getData() 函数中的 print 语句仅适用于循环的第一次迭代。在那之后,什么也没有发生。没有错误。
有什么建议吗?
最佳答案
你不需要再次解码数据
试试这个:
User(dynamic data){
this._id = data["_id"];
this.index = int.parse(data["index"]);
this.about = data["about"];
....
已更新
var jsonBody = json.decode(data.body) as List;
最终更新 我发现了错误,您不需要解析为 int 或 double 它已经解析了,因为它是一个动态对象。
User(Map<String, dynamic> data) {
this._id = (data)["_id"];
this.index = data["index"];
this.about = (data)["about"];
this.name = (data)["name"];
this.picture = (data)["picture"];
this.gender = (data)["gender"];
this.age = (data)["age"];
this.registered = (data)["registered"];
this.longitude = data["longitude"];
this.email = (data)["email"];
this.eyeColor = (data)["eyeColor"];
this.phone = (data)["phone"];
this.address = (data)["address"];
this.latitude = (data)["latitude"];
this.balance = (data)["balance"];
this.guid = (data)["guid"];
this.company = (data)["company"];
this.isActive = (data)["isActive"];
}
关于class - 类的对象未在 Flutter 的循环中实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51754335/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案