How to use the PI constant in Dart - dart

I imported math.
import 'dart:math';
But how can I call "PI" constant?
This is not working.
math.pi / 12.0

you should
import 'dart:math' as math; instead of just import 'dart:math';
because when you use the as keyword you provide the imported library a name so you can reference it anywhere in your file

As an alternative to the accepted answer, you can keep importing without a prefix, and reference pi as just pi:
import "dart:math" show pi;
main() {
print(pi / 12);
}
This works just as well as prefixing. It's a matter of taste which one you prefer.

First import 'dart:math';
then use pi/12.0 instead of math.PI/12.0 it should work fine.

Related

Does Dart have import alias?

I found myself written tedious code when importing files into dart files like the following:
import '../../constants.dart';
I'm wondering if there is any way to create an alias to specific folder like:
import '#shared/constants.dart';
Thanks,
Javi.
Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix
import '../../constants.dart' as foo;
...
foo.ImportedClass foo = foo.ImportedClass();
It allows also to filter imported identifiers like
import '../../constants.dart' show foo hide bar;
See also
https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
What is the difference between "show" and "as" in an import statement?
Barrel files can also make importing easier like
lib/widgets/widgets.dart
export 'widget1.dart';
export 'widget2.dart';
export 'widget3.dart';
export 'widget4.dart';
lib/pages/page1.dart
import '../widgets/widgets.dart';
Widget build(BuildContext context) => Widget1();
No. Dart do not have import alias.
But you have absolute imports which makes up for it:
import 'package:my_lib/shared/constants.dart
This will import the file /lib/shared/constants.dart

Forward package member in dart

I import a package and would forward a member of it. What syntax does Dart offer for that?
foo.dart
import 'package:xzy/xzy.dart'; // contains class Xyz
// how do I forward Xyz here to make it available in bar.dart?
bar.dart
import 'package:mypackage/foo.dart';
Xyz is hidden here
You can use export
import 'package:xzy/xzy.dart';
export 'package:xzy/xzy.dart' show Xyz;
or just
export 'package:xzy/xzy.dart' show Xyz;
You don't need to import for that. Just export is enough if you don't need Xyz in this re-exporting file.
See also
https://www.dartlang.org/guides/libraries/create-library-packages
What is the difference between "show" and "as" in an import statement?

How to use HashLocationStrategy for router in angular2-beta.20 in dart?

I tried to use the router in angular2-beta.20 in Dart with the HashLocationStrategy.
But I couldn't find any docs, except for
this link to angular2-beta.15 docs, which are incomplete.
The example shows TypeScript imports instead of Dart ones.
So I tried to import package:angular2/router.dart, but the Dart Analyzer keeps complaining that it can not find LocationStrategy and HashLocationStrategy
Also I don't know, how to write the import exactly, because a top-level provide function, as in the example above, seems non existent.
provide(LocationStrategy, {useClass: HashLocationStrategy})
After some research I found the following:
LocationStrategy and HashLocationStrategy are now part of
package:angular2/platform/common.dart instead of package:angular2/router.dart.
The bootstrap()- method is platform specific, so we need to import package:angular2/platform/browser.dart.
We need to import package:angular2/router.dart to have ROUTER_PROVIDERS available in bootstrap() method.
Here is a working code example for the dart file initializing :
// needed to import "bootstrap" method
import 'package:angular2/platform/browser.dart';
// needed to import LocationStrategy and HashLocationStrategy
import 'package:angular2/platform/common.dart';
// needed for Provider class
import 'package:angular2/angular2.dart';
// needed to import ROUTER_PROVIDERS
import 'package:angular2/router.dart';
// import your app_component as root component for angular2
import 'app_component.dart';
void main() {
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
const Provider(LocationStrategy, useClass: HashLocationStrategy)
]);
}
Hope this helps somebody! :)

connectWithResult method is undefined for MqttClient object

Below are my imports used in my java program. What I am trying to do is, to create a client of type MqttCient and then use the method connectWithResuls as follows
client.connectWithResult(opts);
the problem is eclipse underscores the lie mentioned above with red squiggle, and I do not know why, I also referred to Paho java docs here.
please let me know why the aforementioned method is not recognised.
Imports
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;

Import C headers in Swift

I cannot import malloc/malloc.h to use malloc_size() to see the size of an object. Is it available in Swift or am I doing it wrong: import malloc/malloc.h
The Unix/POSIX stuff is all in the Darwin module.
import Darwin
let r = rand()
qsort_b(buffer, 0, 10, sortFunc)
malloc is in there, too, but it won't do you much good — it returns an opaque pointer.
Have you looked into using a bridging header?
Create a .h file named -Bridging-Header-File.h
Then reference it in your projects build settings (under "Swift Compiler" look for "Objective C Bridging Header") with:
$(SRCROOT)/<Your-Project-Name>-Bridging-Header.h
Now malloc_size() should be available
You can't import C headers. In order to import a C Library (or Objective-C framework) in Swift it has to be in a module. I'm not aware of a module that contains malloc though.
You want to use "malloc_size"? You do not need to import "malloc.h" to do that, assuming you're either including Foundation or Cocoa.
This works just fine in a playground:
import Foundation
let chars = BytePtr.alloc( 1000 )
let qty_of_bytes_allocated = malloc_size( chars )
println( qty_of_bytes_allocated )

Resources