how can i solve this problem of dart null safety - flutter

this the screenshot of the error that i found

The compiler can't see what type data is, so it considers it as type Object. Object doesn't have a [] operator. Assuming that data is actually a Map you can try to replace
var data = doc.data();
with
Map? data = doc.data() as Map?;
Although there might be a nicer way to write this, but that's hard to know without seeing more of your code. Usually you can indicate the type already somewhere above it by using <Map> at the right place

Related

Dart: Null-safety and retrieving values from maps

there is one thing I can't quite wrap my head around concerning null-safety in Dart, and that concerns how to safely retrieve values from Map<String,dynamic> (I've read the FAQ from the Dart docs).
Basically, the following code in the DartPad with null-safty enabled is valid:
void main() {
int i;
Map<String, dynamic> map = {"key": 1};
i = map["key"];
print(i);
}
Which I do not understand. Why can I assign map["key"] to i without the compiler shouting at me? From the docs:
Code should be safe by default. If you write new Dart code and don’t
use any explicitly unsafe features, it never throws a null reference
error at runtime.
But exactly this is happening. If, in the code above, the key is not in the map, or contains some random type, the program will crash on runtime, which I though is what should never happen with null safety.
I'm particular interested in this since I'm writing a Flutter app and don't understand how to properly deserialize the JSON data I fetch from the DB (try..catch? Special syntax like ??= ?). Even though I don't have the 'non-nullable' language feature enabled (I can't even write int? val without getting a warning), the compiler does not seem to mind that I assign nullable values to non-nullable variables, and will happily crash on runtime if they are null.
Basically, my question does not only concern null-safety, but the type system in general, since from my understanding it shouldn't be possible to assign a dynamic value to an int variable, but obviously this works with Map. Any explanation is greatly appreciated!
A dynamic variable can contain any data type, "dynamic" keyword is used when you don't know the specific data type that might be returned.
and what you're actually doing here:
i = map["key"];
is assigning a "dynamic" variable to an "int" variable and since dynamic in this case the value in the key/value pair of your Map is an integer which matches the data type "int" of variable "i" it won't crash because of type inference done at runtime and not compile time. if the "dynamic" variable was a String it would crash at runtime because of a type mismatch. Hope this is explanatory.

MapEntry - specify type annotation

I'm using a linter package to practice strict coding, however, I can't figure out how to solve this warning. The code is working but I just want to understand this warning and how to solve this. I'm new to flutter by the way. Hope someone can answer my question. TIA!
Actual Code
PS: sorry, unable to display image because of restriction
The lint you're getting is from pedantic's always_specify_types property. It means that you have to specify the type of the variable. In your case, you need to specify the type of your MapEntry. By default, MapEntry's key and value's type will be dynamic. You'd have to specify it as MapEntry<int,Tab>.
In simple terms, instead of:
var number = 12; // dynamic means that the type can be anything (Eg: String, int, etc.)
do:
int number = 12;

Is Map really a type in Flutter? Unfinished Map declarations break other Map declarations

I understand that in Flutter, I can declare a Map using a map constructor':
eg.
var map_name = new Map();
and then use it:
map_name[key] = value
or using Map literals:
var details = {'Username':'Fede','Password':'pass#123'};
However, I have seen perfectly valid code in Dart such as:
Map<String, int> phoneBook ={
'Fede': 12345678,
'Juli': 5467899,
'Pablo' : 56788654,
};
This kind of declaration can be accepted by the compiler in normal cases:
code accepted by compiler
but (after hours of debugging) I have seen that not finishing the declaration of one map in this way by not assigning a name for it, the compiler (in Android Studio) will yield an error telling that "Map isn't a type" in other valid declarations, even in other files calling that file where the Map declaration was not finished! That is, the error is quite spread.
crashed code
map isnt't a type
In other words, the unfinished declaration of one Map breaks the possibility to declare any other Maps in this way, anywhere linked to that unfinished sentence giving a 'map isn't a type' error. The problem dissappears when you just put a name to the unfinished Map declaration and Maps are treated as types again. So my question is: Are Maps a type for Flutter, or is it just a minor bug?
In your "crashed code" picture, i seen that you have not provide name for second map.Your error is kind of syntax, please provide correct declaration to make its work. I tried your code in my IDE and its working perfectly.The syntax you tried is totally valid and it should working.

Swift 2: How to parse a time signature from MIDIMetaEvent?

Before Swift, a MIDIMetaEvent's data was accessed via data[0], data[1], etc.
To get to a time signature I need two values from the data part which is labeled as (UInt8)... with the parens.
But when I try to get the value in this way:
let midiMessage = UnsafePointer<MIDIMetaEvent>(eventData).memory
let data1 = midiMessage.data[0]
This results in an error: "Cannot subscript a value of type 'UInt8' with an index of type 'Int'
Any clue what I've done wrong here? Just getting midiMessage.data only returns the first byte of data.
No snark intended, but file a Radar and ask for an "enhancement" to get rid of using tuples for dynamically sized arrays. They have done this already with some parts of Core MIDI, but not meta events. Or MIDI thru.
It would be nice if they just added the Core MIDI functionality to AVFoundation (they've started) to get rid of the C API altogether.
In the meantime you can go through contortions like this which uses Mirror https://github.com/jverkoey/swift-midi/blob/master/LUMI/CoreMIDI/MIDIPacket%2BSequenceType.swift

string option to string conversion

How do I convert the string option data type to string in Ocaml?
let function1 data =
match data with
None -> ""
| Some str -> str
Is my implementation error free? Here 'data' has a value of type string option.
To answer your question, yes.
For this simple function, you can easily find it in Option module. For example, Option.default totally fits your purpose:
let get_string data =
Option.default "" data
There are many other useful functions for working with option types in that module, you should check them out to avoid redefine unnecessary functions.
Another point is that the compiler would tell you if there's something wrong. If the compiler doesn't complain, you know that the types all make sense and that you have covered every case in your match expression. The OCaml type system is exceptionally good at finding problems while staying out of your way. Note that you haven't had to define any types yourself in this small example--the compiler will infer that the type of data is string option.
A lot of the problems the compiler can't detect are ones that we can't detect either. We can't tell whether mapping None to the empty string is what you really wanted to do, though it seems very sensible.