Flutter Future in Constructor
I have a document in Firestore that has a reference to another document in Firestore, I would like to call the constructor for the first document and it build the referenced document at the same time. It seems I'm stuck with using a Future when I can't use a Future (in a constructor).
Is there any way to have ClassA's constructor convert ClassB's DocumentReference to ClassB (probably via ClassB.fromSnapshot())?
In ClassA I tried a ClassB.fromRef(DocumentReference), but in ClassB that has to return a Future, and as far as I know, you can't do Futures in a constructor. In solutions I've seen, they push the Future part to the UI calling ClassA, but I'm not sure how I would handle a Text widget like that (Text(await classA.classB.toString())?)
I can get the DocumentReference to resolve via a .then(), but I can't call a setState from this model (which makes sense, it's model, not view). The only way I can see it working, is if on my view side, I keep an array of strings to display in a ListView, and do a .then(setState()), but that's really gross...
I guess, what I'm wanting is to be able to do a ListView
(Item
) with ClassA classA = ClassA.fromSnapshot(shapshot);
and be able to call classA.classB.name
.
The 2 classes are as follows:
ClassA.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:msd/models/ClassB.dart';
class ClassA {
/// The firestore ID
String id;
String name;
ClassB classB;
String status;
DocumentReference reference;
ClassA({id, name, classB, status, reference}) {
this.id = id;
this.name = name;
this.classB = classB;
this.status = status;
this.reference = reference;
}
ClassA.fromMap(Map<String, dynamic> map, String id, {this.reference}) {
id = id;
name = map['name'];
status = map['status'];
classB = ClassB.fromRef(map['classB']);
}
ClassA.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
String toString() {
return 'ClassA $name at $ClassB';
}
}
ClassB.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
class ClassB {
/// The firestore ID
String id;
String code;
String email;
String name;
String phone;
DocumentReference reference;
ClassB({id, code, email, name, phone, address, reference}) {
this.id = id;
this.code = code;
this.email = email;
this.name = name;
this.phone = phone;
this.reference = reference;
}
ClassB.fromMap(Map<String, dynamic> map, String id, {this.reference})
: assert(map['code'] != null),
assert(map['email'] != null),
assert(map['name'] != null),
assert(map['phone'] != null),
assert(map['address'] != null),
id = id,
code = map['code'],
email = map['email'],
name = map['name'],
phone = map['phone'];
ClassB.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
static Future<ClassB> fromRef(DocumentReference ref) async {
DocumentSnapshot snapshot = await ref.get();
return ClassB.fromSnapshot(snapshot);
}
String toString() {
return '$name <$code>';
}
}
flutter google-cloud-firestore
add a comment |
I have a document in Firestore that has a reference to another document in Firestore, I would like to call the constructor for the first document and it build the referenced document at the same time. It seems I'm stuck with using a Future when I can't use a Future (in a constructor).
Is there any way to have ClassA's constructor convert ClassB's DocumentReference to ClassB (probably via ClassB.fromSnapshot())?
In ClassA I tried a ClassB.fromRef(DocumentReference), but in ClassB that has to return a Future, and as far as I know, you can't do Futures in a constructor. In solutions I've seen, they push the Future part to the UI calling ClassA, but I'm not sure how I would handle a Text widget like that (Text(await classA.classB.toString())?)
I can get the DocumentReference to resolve via a .then(), but I can't call a setState from this model (which makes sense, it's model, not view). The only way I can see it working, is if on my view side, I keep an array of strings to display in a ListView, and do a .then(setState()), but that's really gross...
I guess, what I'm wanting is to be able to do a ListView
(Item
) with ClassA classA = ClassA.fromSnapshot(shapshot);
and be able to call classA.classB.name
.
The 2 classes are as follows:
ClassA.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:msd/models/ClassB.dart';
class ClassA {
/// The firestore ID
String id;
String name;
ClassB classB;
String status;
DocumentReference reference;
ClassA({id, name, classB, status, reference}) {
this.id = id;
this.name = name;
this.classB = classB;
this.status = status;
this.reference = reference;
}
ClassA.fromMap(Map<String, dynamic> map, String id, {this.reference}) {
id = id;
name = map['name'];
status = map['status'];
classB = ClassB.fromRef(map['classB']);
}
ClassA.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
String toString() {
return 'ClassA $name at $ClassB';
}
}
ClassB.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
class ClassB {
/// The firestore ID
String id;
String code;
String email;
String name;
String phone;
DocumentReference reference;
ClassB({id, code, email, name, phone, address, reference}) {
this.id = id;
this.code = code;
this.email = email;
this.name = name;
this.phone = phone;
this.reference = reference;
}
ClassB.fromMap(Map<String, dynamic> map, String id, {this.reference})
: assert(map['code'] != null),
assert(map['email'] != null),
assert(map['name'] != null),
assert(map['phone'] != null),
assert(map['address'] != null),
id = id,
code = map['code'],
email = map['email'],
name = map['name'],
phone = map['phone'];
ClassB.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
static Future<ClassB> fromRef(DocumentReference ref) async {
DocumentSnapshot snapshot = await ref.get();
return ClassB.fromSnapshot(snapshot);
}
String toString() {
return '$name <$code>';
}
}
flutter google-cloud-firestore
add a comment |
I have a document in Firestore that has a reference to another document in Firestore, I would like to call the constructor for the first document and it build the referenced document at the same time. It seems I'm stuck with using a Future when I can't use a Future (in a constructor).
Is there any way to have ClassA's constructor convert ClassB's DocumentReference to ClassB (probably via ClassB.fromSnapshot())?
In ClassA I tried a ClassB.fromRef(DocumentReference), but in ClassB that has to return a Future, and as far as I know, you can't do Futures in a constructor. In solutions I've seen, they push the Future part to the UI calling ClassA, but I'm not sure how I would handle a Text widget like that (Text(await classA.classB.toString())?)
I can get the DocumentReference to resolve via a .then(), but I can't call a setState from this model (which makes sense, it's model, not view). The only way I can see it working, is if on my view side, I keep an array of strings to display in a ListView, and do a .then(setState()), but that's really gross...
I guess, what I'm wanting is to be able to do a ListView
(Item
) with ClassA classA = ClassA.fromSnapshot(shapshot);
and be able to call classA.classB.name
.
The 2 classes are as follows:
ClassA.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:msd/models/ClassB.dart';
class ClassA {
/// The firestore ID
String id;
String name;
ClassB classB;
String status;
DocumentReference reference;
ClassA({id, name, classB, status, reference}) {
this.id = id;
this.name = name;
this.classB = classB;
this.status = status;
this.reference = reference;
}
ClassA.fromMap(Map<String, dynamic> map, String id, {this.reference}) {
id = id;
name = map['name'];
status = map['status'];
classB = ClassB.fromRef(map['classB']);
}
ClassA.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
String toString() {
return 'ClassA $name at $ClassB';
}
}
ClassB.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
class ClassB {
/// The firestore ID
String id;
String code;
String email;
String name;
String phone;
DocumentReference reference;
ClassB({id, code, email, name, phone, address, reference}) {
this.id = id;
this.code = code;
this.email = email;
this.name = name;
this.phone = phone;
this.reference = reference;
}
ClassB.fromMap(Map<String, dynamic> map, String id, {this.reference})
: assert(map['code'] != null),
assert(map['email'] != null),
assert(map['name'] != null),
assert(map['phone'] != null),
assert(map['address'] != null),
id = id,
code = map['code'],
email = map['email'],
name = map['name'],
phone = map['phone'];
ClassB.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
static Future<ClassB> fromRef(DocumentReference ref) async {
DocumentSnapshot snapshot = await ref.get();
return ClassB.fromSnapshot(snapshot);
}
String toString() {
return '$name <$code>';
}
}
flutter google-cloud-firestore
I have a document in Firestore that has a reference to another document in Firestore, I would like to call the constructor for the first document and it build the referenced document at the same time. It seems I'm stuck with using a Future when I can't use a Future (in a constructor).
Is there any way to have ClassA's constructor convert ClassB's DocumentReference to ClassB (probably via ClassB.fromSnapshot())?
In ClassA I tried a ClassB.fromRef(DocumentReference), but in ClassB that has to return a Future, and as far as I know, you can't do Futures in a constructor. In solutions I've seen, they push the Future part to the UI calling ClassA, but I'm not sure how I would handle a Text widget like that (Text(await classA.classB.toString())?)
I can get the DocumentReference to resolve via a .then(), but I can't call a setState from this model (which makes sense, it's model, not view). The only way I can see it working, is if on my view side, I keep an array of strings to display in a ListView, and do a .then(setState()), but that's really gross...
I guess, what I'm wanting is to be able to do a ListView
(Item
) with ClassA classA = ClassA.fromSnapshot(shapshot);
and be able to call classA.classB.name
.
The 2 classes are as follows:
ClassA.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:msd/models/ClassB.dart';
class ClassA {
/// The firestore ID
String id;
String name;
ClassB classB;
String status;
DocumentReference reference;
ClassA({id, name, classB, status, reference}) {
this.id = id;
this.name = name;
this.classB = classB;
this.status = status;
this.reference = reference;
}
ClassA.fromMap(Map<String, dynamic> map, String id, {this.reference}) {
id = id;
name = map['name'];
status = map['status'];
classB = ClassB.fromRef(map['classB']);
}
ClassA.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
String toString() {
return 'ClassA $name at $ClassB';
}
}
ClassB.dart :
import 'package:cloud_firestore/cloud_firestore.dart';
class ClassB {
/// The firestore ID
String id;
String code;
String email;
String name;
String phone;
DocumentReference reference;
ClassB({id, code, email, name, phone, address, reference}) {
this.id = id;
this.code = code;
this.email = email;
this.name = name;
this.phone = phone;
this.reference = reference;
}
ClassB.fromMap(Map<String, dynamic> map, String id, {this.reference})
: assert(map['code'] != null),
assert(map['email'] != null),
assert(map['name'] != null),
assert(map['phone'] != null),
assert(map['address'] != null),
id = id,
code = map['code'],
email = map['email'],
name = map['name'],
phone = map['phone'];
ClassB.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, snapshot.documentID, reference: snapshot.reference);
static Future<ClassB> fromRef(DocumentReference ref) async {
DocumentSnapshot snapshot = await ref.get();
return ClassB.fromSnapshot(snapshot);
}
String toString() {
return '$name <$code>';
}
}
flutter google-cloud-firestore
flutter google-cloud-firestore
asked Nov 24 '18 at 23:17
unixnerd777unixnerd777
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463224%2fflutter-future-in-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463224%2fflutter-future-in-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown