This is my code.( A detail page for json api response) I'm trying to make color red if negative, and green if positive. My code is :
class StocksDetailScreen extends StatefulWidget {
final String? degisimoran;
final String? sondeger;
final String? hacim;
final String? mindeger;
final String? maxdeger;
final String? hisseismi;
final String? hissekodu;
const StocksDetailScreen({
Key? key,
this.degisimoran,
this.sondeger,
this.hacim,
this.mindeger,
this.maxdeger,
this.hisseismi,
this.hissekodu,
}) : super(key: key);
@override
State<StocksDetailScreen> createState() => _StocksDetailScreenState();
}
class _StocksDetailScreenState extends State<StocksDetailScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.hisseismi!,style: TextStyle(fontSize: 18, fontWeight: FontWeight.w900),),
),
body: SafeArea(
bottom: true,
top: false,
maintainBottomViewPadding: true,
child: Column(
children: [
Expanded(
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Divider(height: 3.6,),
SizedBox(height: 50,),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FadeInUp(
duration: Duration(milliseconds: 1000),
from: 30,
child: Text(
widget.sondeger!,
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: widget.sondeger! > 0
? Colors.green
: Colors.red,
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
),
),
...
On the line:
child: Text(
widget.sondeger!,
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: widget.sondeger! > 0
? Colors.green
: Colors.red,
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
),
I get this error. error: The operator '>' isn't defined for the type 'String'. dart flutter. How can i fix this? Thanks for your help
CodePudding user response:
Hi your variable is a string and in dart you can't do it. Before that you need convert string to integer.
final String? sondeger;
In dart you can convert like that.
int.parse(sondeger)>0
But this variable is nullable and if you are not sure about this variable can be not number. I suggest you to use
int.tryParse(var);
CodePudding user response:
You have the variable sondeger
defined as String?
.
So, you have to parse the String variable into int and compare it.
Example
child: Text(
widget.sondeger!,
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: (int.tryParse("${widget.sondeger}") ?? 0) > 0
? Colors.green
: Colors.red,
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
),
It tries to parse the given string into int; if failed, defaults to 0.
CodePudding user response:
You are using final String? sondeger;
as String, it cant have smaller or greater condition like this. You may int as parameter to access the pass number.
final int? sondeger;
Text(
"${widget.sondeger}",
style: GoogleFonts.poppins(
textStyle: TextStyle(
color: (widget.sondeger??0) > 0 ? Colors.green : Colors.red,
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
);
To receive string follow selimdogan