Reputation: 3
'The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't' pleas can any one help me with this error. i faced this error will trying to make extracting widget
import 'package:flutter/material.dart';
import 'quote.dart';
void main() => runApp(const MaterialApp(
home: QuoteList() ,
));
class QuoteList extends StatefulWidget {
const QuoteList({Key? key}) : super(key: key);
@override
State<QuoteList> createState() => _QuoteListState();
}
class _QuoteListState extends State<QuoteList> {
List<Quote> quotes = [
Quote(text: 'This is the quote number 1',author: 'Ahmed Maged'),
Quote(text: 'This is the quote number 2',author: 'Kareem Khaled'),
Quote(text: 'This is the quote number 3',author: 'Ahmed Shoman'),
];
Widget quoteTemplate(quote){
return QuoteCard(quote: quote);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: const Text(
'List of Data',
style: TextStyle(
color: Colors.white,
letterSpacing: 2.0,
fontWeight: FontWeight.bold,
fontSize: 20.0,
fontFamily:'FiraSans',
),
),
backgroundColor: Colors.black38,
centerTitle: true,
),
body: Column(
children: quotes.map((quote) => quoteTemplate(quote)).toList(),
),
);
}
}
class QuoteCard extends StatelessWidget {
final Quote? quote;
const QuoteCard({super.key, this.quote});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
quote.text, // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 6.0,),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
quote.author, // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 14.0,
fontFamily: 'FiraSans',
),
),
],
),
],
),
),
);
}
}
this is the qoute file quote.dart file:
class Quote {
String? text;
String? author;
Quote({required this.text, required this.author});
}
it just alisson from the net ninja lesson number #20
Upvotes: 0
Views: 426
Reputation: 5792
This error in Flutter occurs when you are passing a nullable string (String?) to a function or method that expects a non-nullable string (String). To resolve the error, you can either make sure the string being passed is not null or update the function or method to accept a nullable string. You can also use the ?? operator to provide a default value for the string in case it is null. try this:
Text(
quote.author??"", // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 14.0,
fontFamily: 'FiraSans',
),
and this
Text(
quote.text, // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
the solution for your commented error is
class QuoteCard extends StatelessWidget {
final Quote quote;
const QuoteCard({super.key,required this.quote});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
quote.text, // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 6.0,),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
quote.author, // error part
style: const TextStyle(
letterSpacing: 2.0,
fontSize: 14.0,
fontFamily: 'FiraSans',
),
),
],
),
],
),
),
);
}
}
Upvotes: 1