oon arfiandwi
oon arfiandwi

Reputation: 555

Import dart:html in Service got exception No provider found for dynamic

I have a working UserService similar to HeroService from AngularDart tutorial with BrowserClient. And now I need to use localStorage to save the API response.

After I import dart:html, I refresh my browser, suddenly I got error on console:

EXCEPTION: No provider found for dynamic: UserService -> dynamic.

When I remove the import, the service running well.

this is my UserService code.

import 'dart:convert';
import 'dart:html';

import 'package:http/http.dart';

import 'package:my_app/src/instance_logger.dart';

class UserService with InstanceLogger {
  static final _headers = {'Content-Type': 'application/json'};
  static const _authUrl = 'https://my.domain/api-token-auth/';
  String get loggerPrefix => 'UserService'; // or set ''

  final Client _http;

  UserService(this._http);

  dynamic _extractData(Response resp) => json.decode(resp.body)['data'];

  Exception _handleError(dynamic e) {
    log(e); // for demo purposes only
    return Exception('Server error; cause: $e');
  }
}

in Component meta-data:

  providers: [ClassProvider(UserService)],

how to use dart:html within service? If I want to access localStorage.

thank you.

Upvotes: 0

Views: 845

Answers (1)

Brian Gorman
Brian Gorman

Reputation: 824

Both dart:html and package:http/http.dart unfortunately have a class named Client.

How you get around this collision is to hide an unwanted class from being imported with its package.

Change

import 'dart:html';

to

import 'dart:html' hide Client;

That will take care of the collision. You could alternatively give the import an alias such as:

import 'dart:html' as html;

Then you can use localStorage by using the prefix html.localStorage;

When I run into this problem I just hide the Client class from the dart:html import.

Upvotes: 3

Related Questions