flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.18k stars 26.64k forks source link

How to use the model attribute in pubspec.yaml #146713

Closed LanSeLianMa closed 1 month ago

LanSeLianMa commented 1 month ago

Please tell me how to use the model attribute in pubspec.yaml. Is there any relevant documentation? I want to develop into modules. I don’t know about this model attribute. Can you provide help?

截屏2024-04-13 13 47 42
LanSeLianMa commented 1 month ago

This document also does not introduce the model attribute.

https://dart.dev/tools/pub/pubspec

AbdeMohlbi commented 1 month ago

@LanSeLianMa where did do u find this model attribute when creating a new flutter project it is missing and it's true that it is missing from the internet also no one knows about it beacuse when searching ur issue appeared as the first search result

LanSeLianMa commented 1 month ago

What I discovered by accident was that I was curious about this module attribute and how to use it.

https://github.com/flutter/samples/blob/main/add_to_app/android_view/flutter_module_using_plugin/pubspec.yaml

AbdeMohlbi commented 1 month ago

i see ,it looks like it has something to do with module packaging

LanSeLianMa commented 1 month ago

Is there any relevant documentation for use?

AbdeMohlbi commented 1 month ago

@LanSeLianMa after some reasearch here is what i've found i hope it helps:

This section in the pubspec.yaml file of a Flutter project is used to define the configuration for embedding your Flutter project as a module within a native host app. Here's what each field means:
androidX: This boolean field indicates whether your Flutter project uses AndroidX libraries. AndroidX is the new package structure introduced by Google to make it easier to include new features and maintain compatibility across different versions of Android. Set this to true if your project is using AndroidX, and false if not.
androidPackage: This field specifies the package name for the Android side of your Flutter module. It follows the typical Java package naming convention in reverse domain notation. For example, dev.flutter.example.flutter_module_using_plugin.
iosBundleIdentifier: Similar to the androidPackage, this field specifies the bundle identifier for the iOS side of your Flutter module. It's typically in reverse domain notation, but without the additional package structure levels. For example, dev.flutter.example.flutterModuleUsingPlugin.
These identifiers are important for maintaining consistency and compatibility when adding or modifying assets and plugins within your Flutter project. They ensure that the tooling can correctly integrate your Flutter module into the native host application. However, note that these identifiers don't have any direct impact on the identifiers of your native host application itself, which may have independent or different identifiers.
LanSeLianMa commented 1 month ago

Can Flutter be developed in modules, and each module can run independently, just like Android’s native module development method?

AbdeMohlbi commented 1 month ago

u mean smth like packages that already exist ? edit : u can use pakages in a simular way to store components and also logic fragements to enhance reusability and DRY concept i can provide a simple exemple that i have if ur interested but in this concept not using modules

LanSeLianMa commented 1 month ago

Yes, want them to have reference dependencies

截屏2024-04-14 00 20 51
LanSeLianMa commented 1 month ago

Thanks, I really need relevant reference examples.

AbdeMohlbi commented 1 month ago

@LanSeLianMa sorry for taking so much time run:flutter create modular_flutter_app run:mkdir product_catalog && mkdir shopping_cart run :cd product_catalog flutter create --template=package . cd ../shopping_cart flutter create --template=package . pubspec.yaml file section :

dependencies:
  flutter:
    sdk: flutter
  product_catalog:
    path: ./product_catalog
  shopping_cart:
    path: ./shopping_cart

flutter code :

import 'package:flutter/material.dart';
import 'package:product_catalog/catalog_screen.dart';
import 'package:shopping_cart/cart_screen.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Modular Flutter App'),
        ),
        body: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => CatalogScreen()),
                );
              },
              child: Text('Open Product Catalog'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => CartScreen()),
                );
              },
              child: Text('Open Shopping Cart'),
            ),
          ],
        ),
      ),
    );
  }
}

CatalogScreen and CartScreen are inside this two packages (i don't know where are the exact files currently tbh ) (u can define logic to communicate with native api for exemple i have recently run into perfmonace issue when trying to parse a large json file so i decided to use go for this and reduce the loading time .) for more info hit : flutter docs about that i hope this help

LanSeLianMa commented 1 month ago

Thanks, I'll try it

AbdeMohlbi commented 1 month ago

Yes, want them to have reference dependencies

截屏2024-04-14 00 20 51

i think u have to think in a differnt way bacause this appraoch is not for flutter projects u must use packages to devide the work into sections and treat each section alone ,smth like : home component , models component .. each component will be inside a package (if ur project is that big to make sepearte components ) and models component that will contain all components .. @LanSeLianMa note that the code given is the max simple so it's just to understand the idea only

darshankawar commented 1 month ago

@LanSeLianMa I think it is better to ask about it in flutter's discord help channel. I'll close it from here as this is more of a how to question with support comments thread instead of a bug report of feature request. If you disagree, write in comments and I'll reopen it.

github-actions[bot] commented 2 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.