im new to Dart-flutter.
i have watching a tutorial video from udemy course writing in dart pad
so i also wrote in dartpad. but it showing error in it.
this is the code wrote in udemy..
import 'dart:convert';
void main(){
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
var imageModel = new ImageModel.fromJson(parsedJson);
print(imageModel.url);
}
class ImageModel{
int id;
String url;
ImageModel.fromJson(parsedJson) {
id = parsedJson['id'];
url = parsedJson['url'];
}
ImageModel(this.id, this.url);
}
in that video it runs, but for me it shows error as
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:15:3:
Error: This constructor should initialize field 'id' because its type 'int' doesn't allow null.
ImageModel.fromJson(parsedJson) {
^
lib/main.dart:12:7:
Info: 'id' is defined here.
int id;
^^
lib/main.dart:15:3:
Error: This constructor should initialize field 'url' because its type 'String' doesn't allow null.
ImageModel.fromJson(parsedJson) {
^
lib/main.dart:13:10:
Info: 'url' is defined here.
String url;
^^^
Error: Compilation failed.
i have no idea what the problem is.. can you guys help me to troubleshoot the error
CodePudding user response:
Since you're compiling with null safety, you may change to this:
int? id;
String? url;
For further info about null safety, please refer to this link https://dart.dev/null-safety
CodePudding user response:
Try using null safe code: eg:
int? id; String? url;
CodePudding user response:
You will try like this
import 'dart:convert';
class ImageModel{
final int id;
final String url;
ImageModel({required this.id, required this.url});
factory ImageModel.fromJson(Map<String, dynamic> parsedJson) {
return ImageModel(
id: parsedJson['id'] as int,
url: parsedJson['url'] as String,
);
}
}
void main() {
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
print(parsedJson['url']);
print(parsedJson['id']);
}
CodePudding user response:
You are probably watching tutorial that was recorded before null-safety introduction in Dart (null-safety is a Dart feature that helps prevent NullRefference exceptions in runtime).
You can either go with nullable values (like @Sowat Kheang suggested in another answer) for id
and url
in ImageModel
class (change String
to String?
and int
to int?
). Then you will be fine, but it defeats the purpose of null-safety.
Or you can change your code so it will handle null values in proper way. Code will look something like this:
import 'dart:convert';
void main(){
var rawJson = '{"url": "https://helo.com","id": 2}';
var parsedJson = json.decode(rawJson);
var imageModel = ImageModel.fromJson(parsedJson);
print(imageModel.url);
}
class ImageModel{
// Make your fields 'final' if you don't plan to change them later
// this will make your life easier and is a good practice.
final int id;
final String url;
// we add curly braces to make arguments named
// and add 'required' keyword to indicate that constructor can't be called
// without actually passing arguments into it
ImageModel({required this.id, required this.url});
// here we use factory constructor that will call our default constructor
// it's also a good practice to specify type of a variable if you know it (Map<String, dynamic> in this case)
factory ImageModel.fromJson(Map<String, dynamic> parsedJson) {
// since we don't know for sure if 'parsedJson' has key 'id'
// we would add '??' — null checking operator.
// So if parsedJson['id'] is null, value -1 will be passed
// to the constructor. Same with 'parsedJson['url']'.
return ImageModel(
id: parsedJson['id'] ?? -1,
url: parsedJson['url'] ?? '');
}
}
CodePudding user response:
var rawJson = '{"url": "https://helo.com","id": 2}';
Actually it is a Sting Json and Encoding is not perform properly So follow the step..
Map data={"url": "https://helo.com","id": 2};
- Encode that data.
json.encode(data);
- Now the Encoding is done then you can send the data any server if you have..
- When you get back data then...
json.decode(rawData)
- Then parsing work properly