Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare const String from const String

Tags:

dart

const String IP_ADDRESS = "http://192.168.1.103:8088/";
const String HOME_EXPECTED = IP_ADDRESS + "index.html";

This code returns unexpected error message from Dart Editor. An expression of type 'num' was expected Why? and how can I fix it?

I tried using 'final', 'final const', and static. but failed :(

like image 430
Sungguk Lim Avatar asked Feb 13 '15 07:02

Sungguk Lim


People also ask

How do you declare a string const?

To define a string constant in C++, you have to include the string header library, then create the string constant using this class and the const keyword.

Can you declare string constants in C?

C string constants can be declared using either pointer syntax or array syntax: // Option 1: using pointer syntax. const char *ptr = "Lorem ipsum"; // Option 2: using array syntax.

Can you use #define for strings?

You can use the #define directive to define a string constant.

Can you use const string?

Note also that you cannot use the const keyword to create a constant object. The const keyword can only be applied to the primitive data types (such as ints, floats, chars, and booleans) and strings. Let's understand the use of const with an example.


Video Answer


1 Answers

Update

http://dartbug.com/15853 says

So String+ String is a constant (and has been for a while). String* int isn't and is not expected to be.

I created http://dartbug.com/22408

Original

In Dart it is very limited how you can construct consts. The + operator on String isn't whitelisted for const creation.

Try this instead:

const String HOME_EXPECTED = "${IP_ADDRESS}index.html";

or

final String HOME_EXPECTED = IP_ADDRESS + "index.html";

if const is not required.

like image 172
Günter Zöchbauer Avatar answered Oct 01 '22 18:10

Günter Zöchbauer