Hello everybody working on a project and get this code from a repo and have some types errors and I cant understand them because I cant have the knowledge to solve and I didnt find nothing on google about these errors.
The problem is the @require this.#property and error as null value. I cant understand the problem, can explain me the problem?
Home Widget
class Home extends StatelessWidget {
const Home({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
AppColors.backgroundFadedColor,
AppColors.backgroundColor,
],
stops: [0.0, 1],
),
),
),
SafeArea(
child: _TodoListContent(
todos: fakeData,
),
),
const Align(
alignment: Alignment.bottomRight,
child: AddTodoButton(),
)
],
),
);
}
}
class _TodoListContent extends StatelessWidget {
const _TodoListContent({
Key? key,
@required this.todos,
}) : super(key: key);
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: todos.length,
padding: const EdgeInsets.all(16),
itemBuilder: (context, index) {
final _todo = todos[index];
return _TodoCard(todo: _todo);
},
);
}
}
class _TodoCard extends StatelessWidget {
const _TodoCard({
Key? key,
@required this.todo,
}) : super(key: key);
final Todo todo;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
HeroDialogRoute(
builder: (context) => Center(
child: _TodoPopupCard(todo: todo),
),
),
);
},
child: Hero(
tag: todo.id,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Material(
color: AppColors.cardColor,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
_TodoTitle(title: todo.description),
const SizedBox(
height: 8,
),
if (todo.items.length != 0) ...[
const Divider(),
_TodoItemsBox(items: todo.items),
]
],
),
),
),
),
),
);
}
}
class _TodoTitle extends StatelessWidget {
const _TodoTitle({
Key? key,
@required this.title,
}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Text(
title,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
);
}
}
class _TodoPopupCard extends StatelessWidget {
const _TodoPopupCard({Key key, this.todo}) : super(key: key);
final Todo todo;
@override
Widget build(BuildContext context) {
return Hero(
tag: todo.id,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Material(
borderRadius: BorderRadius.circular(16),
color: AppColors.cardColor,
child: SizedBox(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_TodoTitle(title: todo.description),
const SizedBox(
height: 8,
),
if (todo.items.length != 0) ...[
const Divider(),
_TodoItemsBox(items: todo.items),
],
Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(8),
),
child: const TextField(
maxLines: 8,
cursorColor: Colors.white,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(8),
hintText: 'Write a note...',
border: InputBorder.none),
),
),
],
),
),
),
),
),
),
);
}
}
class _TodoItemsBox extends StatelessWidget {
const _TodoItemsBox({
Key? key,
@required this.items,
}) : super(key: key);
final List<Item> items;
@override
Widget build(BuildContext context) {
return Column(
children: [
for (final item in items) _TodoItemTile(item: item),
],
);
}
}
class _TodoItemTile extends StatefulWidget {
const _TodoItemTile({
Key? key,
@required this.item,
}) : super(key: key);
final Item item;
@override
_TodoItemTileState createState() => _TodoItemTileState();
}
class _TodoItemTileState extends State<_TodoItemTile> {
void _onChanged(bool val) {
setState(() {
widget.item.completed = val;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
leading: Checkbox(
onChanged: _onChanged,
value: widget.item.completed,
),
title: Text(widget.item.description),
);
}
}
On classes properties @required this.#
error: The parameter '#' 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.
Models file
import 'package:meta/meta.dart';
class Todo {
const Todo({
@required this.id,
@required this.description,
this.items,
});
final String id;
final String description;
final List<Item> items;
}
class Item {
Item({
@required this.id,
this.description = '',
this.completed = false,
});
final String id;
final String description;
bool completed;
}
On code
Todo
@required this.id,
@required this.description,
this.items,
and
Item
@required this.id,
error: The parameter '#' 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.
TLDR: change @required
=> required
You are working with "null safety" enabled. This is a good thing, and helps avoid bugs by catching them at compile time.
In Todo
, you have a field final String id;
. With null safety enabled, null
is not a valid value for a String
(i.e. String id = null;
is a compile time error).
This means that you can safely call methods on id
without worrying about it being null
, but it also means you must give it a value.
Consider:
final todo = Todo();
print(todo.id); // what happens here?
If the compiler allowed your code, this would print "null". But id
is a String
, not a String?
(a nullable string), so this can't be allowed.
The main issue you are facing is the use of @required
rather than just required
. @required
is a metadata annotation, that allows development tools (e.g. your IDE) to give helpful warnings.
On the other hand, required
is a language keyword (when null safety is enabled). If you have a non-null named parameter, you must either:
required
so it is a compile time error if you fail to provide itCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments