Dart converting Future function to a generic Future function - dart

I'm trying to convert one of my functions that takes something from Firebase and converts it into a list of models. I noticed I keep using this logic so maybe the best way to refactor it was to use generics.
My original code:
Future<List<Guideline>> getList ({DatabaseReference query}) async {
DataSnapshot snap = await query.once();
Map<dynamic, dynamic> map = snap.value;
_myList = new List<MyModel>();
map.forEach((key, value) {
_myList.add(MyModel.fromJson(key: key, snapshot: value));
});
return _myList;
}
I tried to use this similar logic to create a generic version of it but I got an error while trying to make it work and I couldn't find a similar problem while searching here so I decided to ask out.
Future<List<T>> getList<T>({DatabaseReference query}) async {
DataSnapshot snap = await query.once();
//dynamic generates an error: The name 'dynamic' isn't a type so it can't be used as a type argument.
Map<dynamic, dynamic> json = snap.value;
return list;
}
If I hadn't gotten the error above I would continue my code as how I did with my first function except that fromJson would be a callback function so I could pass its implementation.

Related

How to convert list to map in dart?

I'm new to dart. I'm trying to covert my Holiday class to Map to be used in my calendar. I tried using Map.fromIterable but it only convert it to <String, dynamic>?
class Occasion {
final List<Holiday> holidays;
Map<DateTime, List> toMap() {
var map = Map.fromIterable(holidays,
key: (e) => DateFormat('y-M-d').format(DateTime.parse(e.date)),
value: (e) => e.name);
print(map);
}
}
class Holiday {
final String date;
final String name;
Holiday({
this.date,
this.name,
});
factory Holiday.fromJson(Map<String, dynamic> parsedJson) {
return Holiday(date: parsedJson['date'], name: parsedJson['name']);
}
}
There are two things:
First: The type parameters of your returned map aren't right for the values you produce in fromIterable. You say it should be List, but in value: ... you are only producing a single String.
Secondly, as I said in my comment you need to help out the dart compiler here a little bit. The compiler isn't very smart. It doesn't see that you are only producing Strings in value. You need to tell him that.
To be fair. This might not be the problem of the compiler, but an overuse of the dynamic type in the collections library.
Map<String, String> toMap() {
var map = Map<String, String>.fromIterable(holidays,
key: (e) => e.date,
value: (e) => e.name );
return map;
}
Just remember: be precise with your types. If you run into type errors start putting additional type information everywhere you can. If you feel it's to cluttered after that, try removing them one spot at a time and see where it leads you.

Flutter Dart: Future.wait() pass value by reference?

In a scenario like the following, how to pass a variable List<Document> itemList by reference ?
Future.wait([futDocs]).then((dataRet){
dataRet.forEach((doco) {
var docList = doco.documents;
docList.forEach((doc){
var docTitle = doc['title'];
print("data is $docTitle");
itemList.add(docTitle); <--- change not reflected outside this function
The itemList does not change when itemList.add(docTitle) is performed in the above Future.wait(). I believe the reason is because itemList is not passed by reference. If I cannot pass itemList by reference, how can I make this work? Can I return dataRet as a list and use it outside Future.wait()?
Adding more info...
Above call is performed within the constructor of class ListBuilder as shown in the following code:
class ListBuilder {
List<Document> itemList = new List<Document>();
ListBuilder(){
var futDocs = Firestore.instance.collection('Data').orderBy('time').limit(10).getDocuments();
Future.wait([futDocs]).then((dataRet) {
dataRet.forEach((doco) {
var docList = doco.documents;
docList.forEach((doc){
var docTitle = doc['title'];
print("data is $docTitle");
itemList.add(docTitle); <--- change not reflected outside this function
[...]
While waiting for an answer, i have tried custom setters and getters as well as the use of this.itemList.add(...) but without success.
Any help, it is much appreciated.
Thanks in advance!
All objects in Dart are passed by reference.
The problem is likely [itemList] isn't updated immediately because Future.wait is an async function. Any code dealing with itemList has to run inside the callback function ( .then((dataret) { ... }); ).
class ListBuilder {
List<Document> itemList = new List<Document>();
ListBuilder() {
var Docs =
Firestore.instance.collection('Data').orderBy('time').limit(10).getDocuments();
Future.wait([futDocs]).then((dataRet) {
dataRet.forEach((doco) {
var docList = doco.documents;
docList.forEach((doc) {
var docTitle = doc['title'];
itemList.add(docTitle);
});
});
/*
itemList has been updated here because we are inside a callback function that
runs _after_ Future.wait has completed.
*/
});
/*
itemList hasn't been updated here _yet_ because this code runs immediately,
regardless how long the Future.wait call takes to complete.
*/
}
}
If you want you can use async/await logic instead of a callback function, which is often easier to read and maintain. More info here: https://www.dartlang.org/tutorials/language/futures
More code would be helpful, but the first things that come to mind are these:
It will probably work to call await before your Future.wait so that the asynchronous code is for sure run
This seems less likely, but you may have to call setState when adding your to your list
Edit:
I would suggest code like this:
class ListBuilder {
List<Document> itemList = new List<Document>();
Future<void> myFunc(var futDocs) async {
await Future.wait([futDocs]).then((dataRet){
[...] *Rest of your Future code here*
}
ListBuilder() {
var futDocs = Firestore.instance.collection('Data').orderBy('time').limit(10).getDocuments();
myFunc(futDocs)
[...]
I have solved the problem however it was not related to pass value by reference. Thank you for your answers, they actually got me thinking and pointed me in the right direction.
The following was the solution to a problem which is not the one raised above :D LOL
Tl;dr; answer:
Change the widget to use streamBuilder
Long answer
The problem was due to the fact that changes to a list were not reflected outside the .then() function. Well, the issue itself is not related to how the list was passed to the function but to the use of async/await logic. When the app is executed, it will populate the itemList in Widget with the current items in the list - this is empty when it starts. In the meanwhile, with an async call, firebase fetch the data and list is updated. However, the UI is not changed. Now, setState gave me an idea but was not the solution. In my case, I don't need a stateful Widget but rather the use of StreamBuilder within the creation of the widget. This bind the list to the async call with the use of stream
new StreamBuilder(
stream: Firestore.instance.collection('data').orderBy('time').limit(10).snapshots(),
builder: (context, snapshot) {
return itemList...
Thanks again!

Dart: Type safety for function that takes a function with varying return value as a parameter

I am trying to create a Dart function that essentially wraps other functions with some boilerplate error handling code, and otherwise returns the value returned by the original function. A key requirement is that it should accept functions with multiple different return types, while avoiding duplicating the common error handling logic across multiple different functions. I found one approach that seems to work by using the dynamic type, except that the compiler is not able to detect type mismatches, so they are only caught at runtime.
Is there a better way to accomplish what I'm aiming for here, and particularly in a way that catches type mismatches at compile time?
Below is a simplified example of my code, where the functions compile fine, but at runtime getAString will raise an error Dart Error: Unhandled exception: type 'List<String>' is not a subtype of type 'String'
/// Signature of API function calls
typedef APIFunctionCall = dynamic Function();
dynamic doWithErrorHandling(APIFunctionCall fn, {retries: 2}) async {
for (int attempts = 0; attempts < retries + 1; attempts++) {
try {
return await fn();
}
on Exception catch (e) {
print(
"This is just an example; actual function does a bunch of more specific error handling.");
}
}
}
Future<String> getAString() async {
// Want a function that can support multiple return types but detect type errors
String doesReturnAString = await doWithErrorHandling(() async => 'hello world'); // This runs fine
String doesntReturnAString = await doWithErrorHandling(() async => <String>['hello', 'world']); // This throws an Error
return doesntReturnAString;
}
You can abstract over the return type using a type parameter:
Future<T> doWithErrorHandling<T>(Future<T> fn(), {int retries = 2}) async {
do {
try {
return await fn();
} catch (e) {
// record error.
}
retries--;
} while (retries >= 0);
return null; // or whatever.
}
With that, you can call with any function. In most cases, the type argument can be inferred from the static type of the argument function, or from the type expected by the surrounding context, but if not, you can write it yourself.
Future<String> getAString() async {
String doesReturnAString = await doWithErrorHandling(() async => 'hello world');
// The next line has a compile-time type error!
String doesntReturnAString = await doWithErrorHandling(() async => <String>['hello', 'world']);
return doesntReturnAString;
}
(As an unrelated hint, you should never catch Exception. Dart errors do not implement Exception, they implement Error. Exception is a meaningless marker interface used by some thrown objects that the user is intended to catch and handle, but in that case, you should be catching the particular exception, like on FormatException, not the plain Exception. So, general rule: Never write on Exception).

Dart JSON Decoder

I am surprised that dart does not have a built in object-to-json and json-to-object mapper.
I read that we have to hand code the mapping ourselves, which is not pleasant.
Anyways, although I have not thoroughly tested it for my use case, I found dart-exportable to be very helpful for half of my requirement.
Any suggested package for json to object decoding?
Your best option is to use the Smoke library.
It's a subset of the Mirrors functionality but has both a Mirrors-based and a Codegen-based implementation. It's written by the PolymerDart team, so it's as close to "Official" as we're going to get.
While developing, it'll use the Mirrors-based encoding/decoding; but for publishing you can create a small transformer that will generate code.
Seth Ladd created a code sample here, which I extended slightly to support child-objects:
abstract class Serializable {
static fromJson(Type t, Map json) {
var typeMirror = reflectType(t);
T obj = typeMirror.newInstance(new Symbol(""), const[]).reflectee;
json.forEach((k, v) {
if (v is Map) {
var d = smoke.getDeclaration(t, smoke.nameToSymbol(k));
smoke.write(obj, smoke.nameToSymbol(k), Serializable.fromJson(d.type, v));
} else {
smoke.write(obj, smoke.nameToSymbol(k), v);
}
});
return obj;
}
Map toJson() {
var options = new smoke.QueryOptions(includeProperties: false);
var res = smoke.query(runtimeType, options);
var map = {};
res.forEach((r) => map[smoke.symbolToName(r.name)] = smoke.read(this, r.name));
return map;
}
}
Currently, there is no support to get generic type information (eg. to support List) in Smoke; however I've raised a case about this here:
https://code.google.com/p/dart/issues/detail?id=20584
Until this issue is implemented, a "good" implementation of what you want is not really feasible; but I'm hopeful it'll be implemented soon; because doing something as basic as JSON serialisation kinda hinges on it!
I haven't had the time to complete it yet but dartson is currently working using mirrors. However a better solution would be using a transformer when compiling to JavaScript. https://pub.dartlang.org/packages/dartson

Does the Dart programming language have an equivalent to Javascript's "prototype"?

In Dart, is it possible for a function to have a prototype associated with it?
Example Javascript code:
doStuff.prototype.isDefined = true; //is there anything like Javascript's function prototypes in Dart?
function doStuff(){
console.log("The function doStuff was called!");
}
Is it possible to do the equivalent of this in Dart (i.e., create a list of properties for each function?)
Two things to address here:
First, Dart doesn't have prototypes or prototypal inheritance, and instead uses classical inheritance. Rather than a prototype, objects have a class, and instead of a prototype chain, objects have superclasses.
Second, for your specific case, I think we'd have to see more of what you need to do to figure out the idiomatic way to do it in Dart. It should soon be possible to emulate functions with objects so that you can invoke an object and still have state and other methods associated with it.
See this article for more: http://www.dartlang.org/articles/emulating-functions/
When that capability lands you'll be able to do this:
class DoStuff {
bool isDefined = true;
call() => print("The function doStuff was called!");
}
var doStuff = new DoStuff();
main() => doStuff();
Which works if you have a fixed set of metadata about your function that you need to keep track of. It's slightly different from JavaScript because each instance of the function in Dart will have its own state for isDefined. I'm not sure if it's possible or easy to get multiple instances of the function in JavasScript, but you might need to make isDefined static so that the value is shared across all instances.
Dart does not allow you to add or remove member variables from an instance of a class at runtime. Rewriting your example in Dart it might look something like this:
class doStuff {
bool isDefined;
doStuff() {
isDefined = true;
}
void stuff() {
print('The function stuff was called!');
}
}
main() {
new doStuff().stuff();
}
If you wanted to add a property bag to a class in Dart you would write:
class PropertyObject {
Map<String, Dynamic> properties;
PropertyObject() {
properties = new Map<String, Dynamic>();
}
Dynamic operator[](String K) => properties[K];
void operator[]=(String K, Dynamic V) => properties[K] = V;
}
main() {
PropertyObject bag = new PropertyObject();
bag['foo'] = 'world';
print('Hello ${bag['foo']}');
}
Note that you can't access map properties using the '.' operator.

Resources