This question already has answers here:
The best way to parse a JSON in Dart
(6 answers)
Closed 3 years ago.
what is the header library for JSON in dart language?
I need to parse from string and convert it into map.
var jsonData = JSON.parse('{"x":1, "y":2}');
what is the header library?
To parse JSON, you import dart:convert and use json.decode (or jsonDecode, which is a just a shorthand for the former).
Related
This question already has answers here:
Null aware function invocation operator
(2 answers)
Closed 3 months ago.
Does the Dart language have anything better to handle the potential of the callback being null than I have done here:
if(widget.onChanged!=null){
widget.onChanged!(value);
}
In case onChanged is of type Function, you can use
onChanged?.call(value);
This will only call the function if it is not undefined/null.
This question already has an answer here:
What are the semantics of _internal?
(1 answer)
Closed 4 years ago.
I have the following code in a flutter tutorial that discusses global state:
static GlobalState instance = new GlobalState._();
GlobalState._();
What exactly is the ._ doing? Is this technically just a blank method?
It's a private constructor. This pattern is used to prevent clients from instantiating or subclassing GlobalState, so the only available instance of GlobalState in the program is instance.
This question already has answers here:
What are function typedefs / function-type aliases in Dart?
(7 answers)
Closed 4 years ago.
This seems like a very obvious question to me, but I cannot seem to find the answer.
How do I write a function type in Dart.
I can use some canned forms like ValueChanged<T> or ValueSetter<T>, but how are they defined? How can I write general function types like T -> U -> V
If I understood your question correctly, I think you are referring to Generic Types. These types are used by functions when you are not sure which type your function is going to use in order to avoid code duplication. For example, let's say you have a class with a method that stores numbers in a List:
class StoreInt {
storeInList(int value);
}
But later, you decide you now want it to store Strings, so you create another class:
class StoreString {
storeInList(String value);
}
Generics allow you to write this class once and use it to store any kind of object you want:
class Store<T> {
storeInList(T value);
}
As far as I know, you don't create generics in dart, you just specify a single letter in between diamond operators as shown in the previous example.
You can find more information on generics here: https://www.dartlang.org/guides/language/language-tour#generics
This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Instance member cannot be used on type of custom class
(3 answers)
Closed 6 years ago.
I'm currently working on learning Swift 3 and I'm trying to implement an XML parser that parses data from a URL. I've created a separate file to handle the XML parsing and have setup it as follows.
let xmlDoc = NSURL(string: "http://labs.bible.org/api/?passage=random&type=xml")
var parser1 = XMLParser(contentsOf: xmlDoc)
I'm currently having issues with syntax however. Swift is highlighting the line where I initialize the XML Parser and giving the error "Cannot use instance member 'xmlDoc' within property initializer, property initializers run before 'self' is available.
This is probably something really simple but I wasn't able to find a solution in my google searches.
Any feedback is appreciated!
-Alex
This question already has an answer here:
How do I lookup a string constant at runtime in Objective-C?
(1 answer)
Closed 8 years ago.
Is it possible to dynamically reference a constant in objective c:
e.g. NSForegroundColorAttributeName is declared in UIKit/NSAttributedString.h as a const NSString. Its value is #"NSColor". In compiled code, I just specify the constant to use it, but what if I need to refer to it dynamically at run time (say a user might type it out)?
I could just use #"NSColor", but that may change as the SDK changes.
Is there a function like NSClassFromString, say ConstFromString:
ConstFromString(#"NSForegroundColorAttributeName") --> #"NSColor"
If the symbol is a public, exported symbol, then you might be able to look it up at runtime using dlsym and friends, but I agree with the commenter that this sounds like a bad idea.