Giter Club home page Giter Club logo

Comments (7)

moffatman avatar moffatman commented on June 14, 2024 1

'$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.

AbdeMohlbi avatar AbdeMohlbi commented on June 14, 2024

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.

definev avatar definev commented on June 14, 2024

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.

AbdeMohlbi avatar AbdeMohlbi commented on June 14, 2024

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.

AbdeMohlbi avatar AbdeMohlbi commented on June 14, 2024

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.

AbdeMohlbi avatar AbdeMohlbi commented on June 14, 2024

Something more intersseting is for exemple :
'$title'.length doesn't throw a runtime exception and have a value of 1 😐?

from flutter.

AbdeMohlbi avatar AbdeMohlbi commented on June 14, 2024

I guess that's it

from flutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.