Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to split dart class in flutter?

Tags:

flutter

dart

I did the following test, but it doesn't work:

//main.dart
class Test
{
  static const   a = 10;
  final b = 20;
  final c = a+1;

}

//part.dart
part of 'main.dart';
class Test
{
  final d = a +1;   //<---undefined name 'a'
} 

I would like to split the class in flutter tutorial into multiple files. For example: _buildSuggestions in a separate file, _buildRow in a separate file, etc.

update:

my solution:

before:

//main.dart
class RandomWordsState extends State<RandomWords> {
{
    final _var1;
    final _var2;
    @override
    Widget build(BuildContext context) {
      ...
      body: _buildList(),
    );

    Widget _buildList() { ... }
    Widget _buildRow() { ... }
}

after:

//main.dart
import 'buildlist.dart';
class RandomWordsState extends State<RandomWords> {
{
    final var1;
    final var2;
    @override
    Widget build(BuildContext context) {
      ...
      body: buildList(this),
    );

}

//buildlist.dart
import 'main.dart';

  Widget buildList(RandomWordsState obj) {
     ... obj.var1 ...
  }
like image 270
camino Avatar asked Apr 01 '18 02:04

camino


People also ask

How do you separate Dart strings?

Dart Split String You can split a string with a substring as delimiter using String. split() method. The function returns a list of strings.

What is split in Flutter?

split method Null safetySplits the string at matches of pattern and returns a list of substrings. Finds all the matches of pattern in this string, as by using Pattern. allMatches, and returns the list of the substrings between the matches, before the first match, and after the last match. const string = 'Hello world!

How do you split a string after a comma in Flutter?

To split a string by comma, use the split method with comma ( , ) as the argument.

How do you extend a class in darts?

In Dart, the extends keyword is typically used to alter the behavior of a class using Inheritance. The capability of a class to derive properties and characteristics from another class is called Inheritance. It is ability of a program to create new class from an existing class.


1 Answers

Dart doesn't support partial classes. part and part of are to split a library into multiple files, not a class.

Private (identifiers starting with _) in Dart is per library which is usually a *.dart file.

main.dart

part 'part.dart';

class Test {
  /// When someone tries to create an instance of this class
  /// Create an instance of _Test instead
  factory Test() = _Test;

  /// private constructor that can only be accessed within the same library
  Test._(); 

  static const   a = 10;
  final b = 20;
  final c = a+1;
}

part.dart

part of 'main.dart';
class _Test extends Test {
  /// private constructor can only be called from within the same library
  /// Call the private constructor of the super class
  _Test() : super._();

  /// static members of other classes need to be prefixed with
  /// the class name, even when it is the super class
  final d = Test.a +1;   //<---undefined name 'a'
} 

A similar pattern is used in many code-generation scenarios in Dart like in

  • https://pub.dartlang.org/packages/built_value
  • https://pub.dartlang.org/packages/built_redux
  • https://pub.dartlang.org/packages/json_serializable

and many others.

like image 153
Günter Zöchbauer Avatar answered Sep 25 '22 12:09

Günter Zöchbauer