Can Dart conditional imports use names? - flutter

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();
}

Related

'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(...)

Same class name for two packages

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.

Unable to import ChannelSftp.LsEntry from Jsch when I use it in Scala code, why?

So in my scala class, I had to use the Jsch(JAVA) library to do SFTP work. But for some reason, it is unable to import:
import com.jcraft.jsch.ChannelSftp.LsEntry
Any idea why this would be? LsEntry is a nested class of ChannelSftp.
http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelSftp.html
package services.impl
import java.nio.file.Path
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.ChannelSftp.LsEntry
import services.InputService
class InputServiceImpl extends InputService[List[ChannelSftp.LsEntry]] {
}
Figured it out.
In scala, to reference an nested class, you use the following syntax:
ChannelSftp#LsEntry

Usage of the `import` statement

Can someone explain me how the import statement works ?
For example I have a type User in the myapp/app/models package:
package models
type User struct {
// exportod fields
}
I have a type Users in the myapp/app/controllers package:
package controllers
import (
_ "myapp/app/models"
"github.com/revel/revel"
)
type Users struct {
*revel.Controller
}
func (c Users) HandleSubmit(user *User) revel.Result {
// Code here
}
This gives me the following error:
undefined: User
I tried to change the imports to the following code:
import (
. "advorts/app/models"
"github.com/revel/revel"
)
But get this error:
undefined: "myapp/app/controllers".User
Which I don't understand either. So, what is the difference between import . "something" and import "something" ? How to properly import my model in my case ?
Each package has a set of types, functions, variables, etc. Let's call them entities. Each entity can be either exported (its name start with an Uppercase letter), or unexported (its name start with a lowercase letter).
A package can only access the exported entites of another package. To do this, it needs to import it, which will make the exported entites available with the package name as identifier. Example:
import "github.com/revel/revel"
will get all exported entites of the revel package, which will be available using revel. prefix. As in revel.Controller, which is the Controller type defined in the revel package.
You can alias a package identifier by prefixing the import path with the wanted identifier. Example:
import rev "github.com/revel/revel"
will import all revel entites with the identifier rev. So revel.Controller becomes rev.Controller. It is useful if you have multiple package with the same name, or a package with an absurdly long name.
As a bonus, you can import a package anonymously, by aliasing it to the blank identifier:
import _ "github.com/revel/revel"
which will import the package, but not give you access to the exported entities. It is useful for things like drivers, which you need to import but never access. A frequent example is the database drivers, which register themselves to the database/sql package so you never need to access them directly.
And as a bonus' bonus, you can also import locally a package, by aliasing it with the . identifier. The exported entites will then be available without identifier, as if you defined them in the same package.
How to properly import your packages is up to you. The general convention is to never alias if you can manage it, to hide the package that you don't need to access but still need to import (database drivers), and that's all. You really never need to import locally a package, even if some tutorials or frameworks do it for simplicity's sake.

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.