简体   繁体   中英

Why does Android Studio Dart Analysis throws error for required keyword?

I have a Coordinate Class where both parameters are required.

Both Android Studio and Visual Studio Code do not recognize the required keyword.

必需的

Why does Dart Analysis give Undefined class required error?

 required isn't a type.
Try correcting the name to match an existing type.
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Coordinate coordinate = Coordinate(lon: 3.4, lat: 2.3);

    return Container(
      child: Text(coordinate.lat.toString()),
    );
  }
}

class Coordinate {
  double lon;
  double lat;
  Coordinate({required lon, required lat});
}

Dart SDK version : 2.12.3 (stable)

Is the version of Dart updated to 2.12 on your environment?

The second error says required is not a valid keyword, the required keyword was added in Dart 2.12.

Note : The compiler is parsing the required keyword as a type and as it does not find the type required defined anywhere in the project it interprets it is as dynamic , Hence the error.

This is the answer to the question

Why does Android Studio Dart Analysis throws error for required keyword?

Now, If it is still not working for you, I suggest you open a new issue with the appropriate tags about the sdk install/environment issues you have. As this question has served its purpose

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