Gustavo Fenilli
Gustavo Fenilli

Reputation: 145

How would you resolve a naming conflict inside dart?

Does dart have some sort of alias for naming conflicts like this?

library flutterfly;

import 'dart:ui';

class Color {
  Color._();
  
  // Color should be the class from dart:ui instead of itself.
  static Color white() {
    return Color(#FFFFFFFF);
  }
}

Upvotes: 3

Views: 1547

Answers (1)

jamesdlin
jamesdlin

Reputation: 90115

Import the library with a namespace/prefix:

import 'dart:ui' as ui;

class Color {
  Color._();

  static ui.Color white() {
    return ui.Color(0xFFFFFFFF);
  }
}

That said, because whoever imports your flutterfly library can choose whatever library prefix they want (if any), adding your own Color class to use as a namespace isn't very useful, and its presence likely would cause conflicts for anything that uses it. Effective Dart recommends against classes that contain only static members. For this example, you could use top-level functions instead.

Upvotes: 3

Related Questions