I want to keep null as a default value for product id, as it'll be later auto-generated, but I can't due to the flutter null safety check.
this is the instance creation code where I want to keep id = null:
var _editedProduct = Product(
id: null,
title: '',
price: 0,
description: '',
imageUrl: '',
);
and here is the code for my Product.dart file:
import 'package:flutter/material.dart';
class Product with ChangeNotifier {
final String id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product({
required this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false,
});
void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}
A screenshot of the error
While you like to have id default value as null, First you need to accept nullable data. For final String id; will be final String? id; the default value can be provided on constructor by removing required to act as optional. id will have null by default.
To use id you need to check if it is null or not.
If you are sure it contains data use ! at the end like id!, or
provide a default value like id??"default", or
if(Product.id!=null){...}
class Product with ChangeNotifier {
final String? id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product({
this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false,
});
...
Learn more about null-safety
Following changes solved my issue:
changing final String id; to final String? id; in Product.dart
changing final productId = ModalRoute.of(context)!.settings.arguments as String; to final String? productId = ModalRoute.of(context)!.settings.arguments as String?;
Related
I am facing issue when I am using JsonSerialization on Objectobox Entity
I tried to find better solution but I could not find
#JsonSerializable()
#Entity()
class ContactModel {
#Id()
int internalId;
#Index()
final String? id;
final String? userId;
final String? businessName;
final String? contactPersonName;
final String? contact;
final String? whatsappNumber;
final String? email;
final String? address;
final String? pinCode;
final String? city;
final String? businessType;
final String? description;
final String? img;
final String? lat;
final String? longitude;
final String? createDate;
final String? clientType;
ContactModel(
{this.internalId=0,
this.id,
this.userId,
this.businessName,
this.contactPersonName,
this.contact,
this.whatsappNumber,
this.email,
this.address,
this.city,
this.pinCode,
this.businessType,
this.description,
this.img,
this.lat,
this.longitude,
this.createDate,
this.clientType});
Map<String, dynamic> toMap(ContactModel contactModel) => _$ContactModelToJson(contactModel);
factory ContactModel.fromJson(Map<String, dynamic> json) => _$ContactModelFromJson(json);
}
I tried making a class then making a list of that class and I get an Erorr
the class I created:
class Transaction {
final String id;
final String title;
final double amount;
final DateTime date;
Transaction({
required this.id,
required this.title,
required this.amount,
required this.date,
});
}
Erorrs I get :
Try changing your variable name as it is clashing with the class name Transaction
and you can also use a different approach:
final List<Transaction>? transactionList;
trnasactionList.add(Transaction(id:1,title:'flutter' ....));
trnasactionList.add(Transaction(id:2,title:'flutter' ....));
import 'package:flutter/foundation.dart';
enum Complexity {
Simple,
Challenging,
Hard,
}
class Meal {
final String id;
final List<String> categories;
final String title;
final String imageUrl;
final List<String> ingredients;
final List<String> steps;
final int duration;
final Complexity complexity;
final bool isGlutenFree;
final bool isLactoseFree;
final bool isVegan;
final bool isVegetarian;
const Meal({
required this.id,
required this.categories,
required this.title,
required this.imageUrl,
required this.ingredients,
required this.steps,
required this.duration,
required this.complexity,
required this.isGlutenFree,
required this.isLactoseFree,
required this.isVegan,
required this.isVegetarian,
});
}
Hallo, i have added the complexity of my meals to my meal app.
But, how can I add the number of persons? What do I need? I want to add, that the meal is for 1 or 2 or 3 persons.
Thank you!
I am following a Flutter tutorial that has such a following code, but code doesn't work on my computer, and I don't know how to fix it:
import 'package:flutter/foundation.dart';
class CartItem {
final String id;
CartItem({
#required this.id,
});
}
But I get such these errors:
The parameter 'id' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
{String id}
The latest dart version now supports sound null safety. The tutorial must be using an old version.
To indicate that a variable might have the value null, just add ? to its type declaration:
class CartItem {
final String? id = null;
CartItem({
this.id,
});
}
or
class CartItem {
final String? id;
CartItem({
this.id,
});
}
You have a few options depending on your own project...
Option1: Make id nullable, you can keep #required or remove it.
class CartItem {
final String? id;
CartItem({
this.id,
});
}
Option2: Give a default (non-null) value to id
class CartItem {
final String id;
CartItem({
this.id="",
});
}
more in this link
You can just replace #required this.id with required this.id.
class City {
int id;
String name;
String imageUrl;
bool isPopular;
City(
{required this.id,
required this.name,
required this.imageUrl,
required this.isPopular});
}
To-Do App
class ToDoItem{
final String id;
final String title;
final Color color;
final int price;
final IconData icon;
final String todoListId;
const ToDoItem({
#required this.color,
#required this.id,
#required this.todoListId,
#required this.price,
#required this.title,
#required this.icon,
});}
I am trying to find
the title of the ToDoItem which has the highest price
And the ToDoItem is being added to this:
List<ToDoItem> toDo= [];
void main(){
List<ToDoItem> _myList = [];
_myList.add(ToDoItem('10',100));
_myList.add(ToDoItem('30',400));
_myList.add(ToDoItem('40',2));
_myList.add(ToDoItem('20',200));
_myList.sort((a,b)=>a.price.compareTo(b.price));
for(ToDoItem items in _myList)
print(items.id + ',' + items.price.toString());
}
class ToDoItem{
final String id;
final int price;
const ToDoItem(
this.id,
this.price,
);
}
This is the code which i used in dart to sort your list . YOu could use the sorting logic from in here . it sorts your list in ascending order acc to price .