R Rifa Fauzi Komara
R Rifa Fauzi Komara

Reputation: 2128

How to call class or functions in another packages to another packages in flutter?

I have a structure project like this:

structure project

I tried to implement modularization on flutter like the one on Android native (Java and Kotlin) where it functions to separate between functions and speed up the build process and simplify future maintenance.

In the image, there is a root project with the name "cari_mobile_flutter_main" and 2 flutter packages with the name "cari_news" and "cari_network". I managed to connect the 2 packages with the root project by setting the path to pubspec.yaml in the root project. So that the lib folder in the root can access/call class and functions from both packages.

The problem is how can each package be connected so that they can call each other's class or function? For example: "cari_news" can call class or functions that are in "cari_network". Can it do that?

Because I have tried to set up the path on pubspec.yaml between the package and it can't and there is an error like this:

this is pubspec.yml from cari_news

I already try to click "packages get" in the top right and get the result like this:

Running "flutter packages get" in cari_news...
Because cari_news depends on cari_network from path which doesn't exist (could not find package cari_network at "cari_network"), version solving failed. pub get failed (66) Process finished with exit code 66

I tried linking cari_network with cari_news so that the cari_news package can call class and functions that are in the cari_network package. But that can't be. Can anyone help me?

Upvotes: 0

Views: 834

Answers (1)

R Rifa Fauzi Komara
R Rifa Fauzi Komara

Reputation: 2128

My mistake, for the answer to my question, I just add ../cari_network to the path section. Because the position of the cari_network package is inside the root project while cari_news is in the root project and the pubspec.yaml is in the cari_news package. So to find the cari_network package just add a ../ only.

Upvotes: 1

Related Questions