Comments (7)
'$title'
means essentially (title).toString()
. And null.toString()
is allowed, it just returns 'null'
as you see.
Sure maybe a lint could be added though, just like we have one to prefer string templating rather than using +
operator.
from flutter.
i was trying to reproduce that in dart :
void main() {
final String? title;
print('$title');
}
the dart analysis and the compiler doesn't allow this but flutter Text widget is allowing the null value to pass and so making the value appearing in the ui null , the problem is i why Text widget is allowing this behaviour ?
from flutter.
This completely correct behavior, your title
variable in this snippet:
void main() {
final String? title;
print('$title');
}
title
variable is in scope of main
function so it will not allow to use if you doesn't assign it yet.
But in your flutter code:
class Home extends StatelessWidget {
const Home({super.key, this.title});
final String? title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('$title'),
centerTitle: true,
),
body: Center(
child: Text(
'$title',
),
),
);
}
}
The title
is param of Home
widget so compiler confidently sure that is assigned (cuz you run the build
function inHome
widget so this must run through constructor of Home
). And the "$title"
is String interpolation it make any value to string so null to String is null
so it right.
The solution easy that you accept only final String title;
and handle the null case outside the Home
widget.
from flutter.
I know that string templating in dart happens in run time not compile time the problem is why not to try adding this feature to the compiler to detect such cases
from flutter.
Also as far as i know in flutter :
Text('$title')
same as Text(title)
This is when title is String
But when using String?
the first one the only one allowed (even if there is a possibility for null case to pass )and second is rejected
from flutter.
Something more intersseting is for exemple :
'$title'.length doesn't throw a runtime exception and have a value of 1 😐?
from flutter.
I guess that's it
from flutter.
Related Issues (20)
- Flutter Web engine removes query parameter encoding HOT 2
- InkWell highlight not working HOT 1
- Navigator without transitions not working HOT 7
- [tool_crash] ProcessException: Process exited abnormally with exit code 72:xcrun: error: unable to find utility "xcodebuild", no
- mac-23 & mac-26 lost external connection phone device. HOT 2
- linux-58 is dead. HOT 2
- mac-8 lost external connection phone device. HOT 2
- mac-2 is low on disk space. HOT 2
- mac-7 & mac-37 lost external connection phone device. HOT 2
- mac-6 is low on disk space. HOT 2
- linux-51 is dead. HOT 1
- [cocoon] Broken builds navigate to builders, not the broken build HOT 4
- linux-37 lost external connection phone device. HOT 1
- Web semantics picks up clicks instead of letting Flutter handle them (?) HOT 1
- Migrate `flutter/engine` to a Dart workspace for Dart/pub packages
- flutter-devicelab-mac-14 is quarantined: No device is available HOT 3
- [Impeller] fix remaining slow frames in flutter gallery application.
- win-12 is low disk space on Temp directory. HOT 1
- [et] Forward `--verbose` to `ninja` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter.