this error is in udemy maximilian schwarzmuller's flutter course error. this is the code: I have provided the string value still I getting nonnull string provided to text widget error in ('tx.title'),
import 'package:flutter/material.dart';
import './transaction.dart';
void main() => runApp(MyWidget());
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter app',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final List<Transaction> transactions = [
Transaction(
id: '01',
title: 'azeem',
amount: 90.98,
date: DateTime.now(),
),
Transaction(
id: '02',
title: 'azeem',
amount: 90.98,
date: DateTime.now(),
)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Expense App'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: double.infinity,
child: Card(
child: Text('Text_1'),
color: Colors.blue,
),
),
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title),
);
}).toList(),
),
],
),
);
}
}
I have provided the string to this function but I am getting an error.
CodePudding user response:
try this;
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title ?? "default text on null"),
);
}).toList(),
),
CodePudding user response:
You have multiple ways to solve this problem
1.
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title!= null ? tx.title : "Default Title"),
);
}).toList(),
),
2.
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title ?? "Default Title"),
);
}).toList(),
),
3.
if(tx.title != null)
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title!),
);
}).toList(),
),
4.
Column(
children: transactions.map((tx) {
return Card(
child: Text(tx.title.toString()),
);
}).toList(),
),