简体   繁体   中英

what does ":" mean or do in Dart?

I have been trying to get an clear explanation for what: means in dart.

As far as I can tell it seems so be some kind of operator, but I can't get a clear cut explanation of what exactly it does.

I understand that it gets used to so assign stuff to other stuff, but I am unsure of the specifics.

I have tried googling it, no luck.

I have tried searching through the dart documentation.

https://dart.dev/guides/language/language-tour#operators

I know that it gets used as part of conditional expressions like here:

https://dart.dev/guides/language/language-tour#conditional-expressions

Here are examples of how it gets used:

Example 1:

class someBloc extends Bloc<someEvent, someState> {
    someBloc() : super(someState.initial());
}

Example 2:

class someClass {
  final String requestType;
  final String name; 
   
  factory someClass.fromJson(Map<String, dynamic> json) {
        return SomeClass(
            requestType: json['Type'],
            name: json['Name']);
      }
}

In examples 1 & 2:

The : is used to set a named parameter. For example, let's say I want to create a Card widget. The card widget optionally takes a named parameter called elevation . Therefore, I can do this:

Card(
  elevation: 4.0,
)

Another way : is used is for writing quick conditional expressions.

final String isMyListEmpty = myList.isEmpty ?? 'Yes, the list is empty' : 'No, the list is not empty';

In this example, our condition is myList.isEmpty . If this condition is true, the first expression after ??is returned. Else, if the condition is false, the expression after the : is returned.

It's just an assignment operator. You assign a value to a widget property. For example, Container widget has many properties, like color or height:

  Container(
    margin: const EdgeInsets.all(10.0),
    color: Colors.amber[600],
    width: 48.0,
    height: 48.0,
  ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM