Same class name for two packages - flutter

I import google_maps_flutter and augmented_reality_plugin_wikitude
Both uses the same name class as CameraPosition.
I don't use CameraPostion class for google_maps_futter.
How can I avoid name comflict??
lib/main.dart:6:1: Error: 'CameraPosition' is imported from both 'package:google_maps_flutter/google_maps_flutter.dart' and 'package:augmented_reality_plugin_wikitude/startupConfiguration.dart'.
import 'package:augmented_reality_plugin_wikitude/startupConfiguration.dart'

You can use as keyword to reference all the respective variables and methods.
For Ex:
import 'package:google_maps_flutter/google_maps_flutter.dart' as cp1;
import 'package:augmented_reality_plugin_wikitude/startupConfiguration.dart' as cp2;
Now, to use CameraPosition or other methods from google_maps_flutter you can use "cp1" reference like cp1.method1().
Similarly, to use CameraPosition or other methods from augmented_reality_plugin you can use "cp2" reference like cp2.method1().
The as keyword's main purpose are typecast and to specify the library prefixes. So this is the best solution for your use case.

Hide CameraPosition from google_maps_flutter while importing
import 'package:google_maps_flutter/google_maps_flutter.dart' hide CameraPosition;

There are 2 ways you could fix this:
Specify an import prefix like import '../../something.dart' as st;
Then use it something like this: st.ImportedClass some = st.ImportedClass();
It also supports import '../../something.dart' show thisthing hide thatthing;
Absolute imports
import 'package:my_lib/shared/something.dart

You can avoid such conflicts by importing one of them in another name. So while importing one of two, let's say you can import google_maps as
import 'package:google_maps_flutter/google_maps_flutter.dart' as gmaps;
So CameraPosition from google maps plugin will be accessed using gmaps.CameraPosition, so there will no longer be any conflicts.

Related

Can Dart conditional imports use names?

Consider a conditional import statement. The following comes from the Dart language guide:
import 'src/hw_none.dart'
if (dart.library.io) 'src/hw_io.dart'
if (dart.library.html) 'src/hw_html.dart';
Is there a syntax to add a name to these imports? For example, I'd like to say something like the following:
import 'src/hw_none.dart' as my_prefix
if (dart.library.io) 'src/hw_io.dart' as my_prefix
if (dart.library.html) 'src/hw_html.dart' as my_prefix;
Unfortunately, the above doesn't compile. I haven't been able to find a variation that does compile.
Is there a way to name conditionally imported packages?
I've done something like this a few month ago, to implement mobile and web functionalities. Seems like you cannot name each import separately.
import 'package:flutter_fcm_web_example/notification_helper.dart';
import 'firebase_mobile_messaging.dart'
if (dart.library.html) 'firebase_web_messaging.dart' as notifInstance;
abstract class NotificationEncapsulation {
static NotificationHelper get instance =>
notifInstance.FirebaseMessagingHelper();
}

'Router' is imported from both 'package:fluro/src/router.dart' and 'package:flutter/src/widgets/router.dart'

I was using Fluro package for navigating. Fluro and Flutter are using 'Router'. So their classes merged. How could I fix this?
lib/routers/routers.dart:2:1: Error: 'Router' is imported from both 'package:fluro/src/router.dart' and 'package:flutter/src/widgets/router.dart'. import 'package:flutter/material.dart'
I faced with that error when upgraded Flutter to 1.20.0.
Change
import 'package:flutter/material.dart'
with
import 'package:flutter/material.dart' hide Router;
in your navigating class that using fluro. So you could use fluro's Router now.
You can alias the Fluro library with the as keyword:
import 'package:flutter/material.dart'
import 'package:fluro/src/router.dart' as Fluro // Alias to avoid conflicts
Then use the alias for any class, method or symbol in the imported library:
var x = Fluro.Router(...)

Undeclared type: createM3FromEclipseProject (Rascal)

In the following module, I tried to add myModel for debugging purpose to see the AST.
module FlowGraphsAndClassDiagrams
import analysis::flow::ObjectFlow;
import lang::java::flow::JavaToObjectFlow;
// Added to check the M3 model
import lang::java::jdt::m3::AST;
import List;
import Relation;
import lang::java::m3::Core;
import IO;
import vis::Figure;
import vis::Render;
import analysis::m3::TypeSymbol;
alias OFG = rel[loc from, loc to];
//To view the M3 model from the whole eclipse project
alias myModel = createM3FromEclipseProject(loc project);
....
When I import the above module in the rascal console, I get the following error:
Reloading module FlowGraphsAndClassDiagrams
|console:///|:Could not load FlowGraphsAndClassDiagrams
|console:///|:could not reimport FlowGraphsAndClassDiagrams
|project://my_project/src/FlowGraphsAndClassDiagrams.rsc|(428,26,<18,16>,<18,42>): Undeclared type: createM3FromEclipseProject
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UndeclaredType/UndeclaredType.html|
I don't understand what the mistake is.
I think the problem lies with
alias myModel = createM3FromEclipseProject(loc project);
What are you trying to achieve with this alias? I think you don't want to use an alias, instead you want to do this:
m = createM3FromEclipseProject(|project://eLib|);
println(m);
http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Declarations/Alias/Alias.html
You can use alias to create a new name for types, while createM3FromEclipseProject(loc project) seems to be a declaration of a function. If you want to call a function, which you seem to be doing, you need to provide a variable/value as parameter: createM3FromEclipseProject(|project://eLib|) if you want to "create an M3" from eclipse project "eLib".
Both answers are right, you can't use aliases for global variables. If you want to make an global variable (which in most cases we advice against), you have to give the type of that variable. We only allow type inference for local variables.
So in your specific case it should be:
M3 myModel = createM3FromEclipseProject(|project://eLib|);
In most cases you want to do this in your main method instead of at module import time.

When exactly do you need to import in java?

The following unexpectedly compiled and ran without a problem:
import info.gridworld.actor.Actor;
import java.util.ArrayList;
public class ChameleonKid extends ChameleonCritter
{
public ArrayList<Actor> getActors()
{
ArrayList<Actor> actors = getGrid().getNeighbors(getLocation());
ArrayList<Actor> frontBack = new ArrayList<Actor>();
for(Actor i : actors)
if(getLocation().getDirectionToward(i.getLocation())==getDirection())
frontBack.add(i);
return frontBack;
}
}
The method getLocation() in the Actor class returns an instance of Location. And then I call the getDirectionToward() method of the Location class. getLocation().getDirectionToward(i.getLocation()). How does this work? I never imported the Location class. How am I able to work with it and call its methods? If that is how it works, when would I need to import a class? Only if I am instantiating it?
I am using Java 7.
Say you have two methods, one returning foo.Location and the other returning bar.Location (two completely different classes happening to have the same name, but in different packages - completely valid):
foo.Location getFooLocation();
bar.Location getBarLocation();
When you can both of these methods in the same class and chain some methods, you don't need an import:
if(getFooLocation().onlyInFoo()) {
//...
}
if(getBarLocation().onlyInBar()) {
//...
}
It works because the compiler is completely sure which version (from which package) of Location are you using and it knows where onlyInFoo() and onlyInBar are available.
But suppose you need a local variable:
Location location;
// much later
location = getFooLocation();
Now the compiler doesn't really know Location do you mean, so you must help him either by proceeding class name with package:
foo.Location location;
or by importing that class:
import foo.Location;
You should now ask: what if I want to have local variable of both foo.Location and bar.Location? Well, you can't import them both, obviously:
//WRONG
import foo.Location;
import bar.Location;
What you can do is again: either don't import at all and use fully qualified names:
foo.Location location1;
bar.Location location;
...or import just one location:
import foo.Location;
//...
Location location1; //foo.Location
bar.Location location2;
The reason why you don't need to import that class is because of the fact that you are not explicitly using that class. Actor is the one actually using the Location class locally. You are never holding a reference to the Location class and so, you are able to keep using it dynamically. However, if you wanted to hold a reference (eg Location l = getLocation();), you would have to import Location since the compiler has to link l with Location, but has no idea where to find it.

Why does my Scala enumeration break when I move it to another package?

Enumeration code looks like the following
package com.mydomain
object Market extends Enumeration {
type Market = Value
val ASX, LSE = Value
}
I try to use as follows
import com.mydomain.Market._
.
.
.
if (Market.ASX == currentMarket) {
...
}
This was working when everything was in the same package. When I moved to a new package I now get
not found: value Market
If you import Market, you have ASX and LSE directly available to you. You don't have Market.ASX available--that would be if you had object Market available, which is what would happen if you did import com.mydomain._.
Being inside package com.mydomain causes com.mydomain._ to be loaded just like you imported it, so that's why you can say Market.ASX when you're in the same package.
When you write code in a different package, you need to either import com.mydomain._ and then use Market.ASX, or import com.mydomain.Market._ and then use ASX.