Comments (8)
'$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.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v
and a minimal reproduction of the issue.
from flutter.
Related Issues (20)
- Add support for pre-publish hooks for publishing first party Flutter packages HOT 1
- [android][platform view]investigate whether android has similar performance issue due to 1 px overlay HOT 3
- [Android] Build the engine against Android 35
- [Android] Maintain current (Android < 35) `SystemUiMode` behavior on Android >= 35 HOT 2
- Flutter supports Sensitive Content Protection HOT 2
- Image picker tests `testPickingFromGallery`, `testPickingWithContraintsFromGallery`, and `testSelectingFromGallery` failed on iOS 18 beta 1 HOT 2
- [tool_crash] _TypeError: (#0 MaterialFonts.updateInner (package:flutter_tools/src/flutter_cache.dart:151:49)) HOT 2
- LicensePage and LicenseRegistry.licenses have license from dev_dependencies in release HOT 1
- NSBundle: principal class is nil because all fallbacks have failed (Add-to-app, IOS 18, XCode 16 Beta 1) HOT 2
- [ios][platform_view]Add another benchmark project with an ad banner anchored on the bottom of the screen
- Framework tree red on `Linux ci_yaml flutter roller` failing HOT 4
- [A11y] Identify Flutter semantics performance issues. HOT 3
- Horizontal `ListView` in nested `ListView`s doesn't work HOT 6
- Linux desktop font rendering is different from Chromium. HOT 4
- [webview_flutter] transfer large data from JS to Dart faster HOT 3
- Building or running flutter macos hello app failed linking HOT 3
- The iOS App crashes after entering the foreground card screen from the background HOT 5
- [tool_crash] HttpException
- Hero widget's child triggers initState repeatedly during route animation without a key HOT 7
- `flutter drive` for Chrome integration tests builds app with Android as the target platform on Linux HOT 2
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.