Home > Enterprise >  dart "part of" can't used twice in a file
dart "part of" can't used twice in a file

Time:12-15

I have a class named _AppColors. it contains all colors used in the application. I want to force developers to read colors from Theme that's why I made this class private. now I want to access this class from two files. but it can be only part of one file. how can I handle this?

app_colors.dart:

part of 'color_extention.dart';

class _AppColors {
   ...

app_theme.dart

import 'package:flutter/material.dart';

part 'app_colors.dart';

extension MultiThemeColorExtension on ThemeData {
   ...

color_extention.dart

import 'package:flutter/material.dart';

part 'app_colors.dart';

const TextTheme _textThemeLight = TextTheme(
   ...

but I have to add part of 'color_extention.dart'; in app_colors.dart which I can't. any solution?

CodePudding user response:

Private symbols are private to the library. Normally a Dart library is a single .dart file, but part is used to specify that a library comprise multiple .dart files (and conversely, part of is used to specify that a .dart file is part of the specified library).

It doesn't make sense for a Dart file to be part of multiple libraries. If you want to share a private class with multiple .dart files, your typical options are:

  •  Tags:  
  • dart
  • Related