The method 'post' was called on null - dart

I want to login to a server and I need to post a username and password.
First of all, how can I pass the two parameters? I tried inside the body like this:
body: {
"login": username, "password": password
}
and
final String param = "login=$username&password=$password";
I created my function like this and always got "NoSuchMethodError: The method 'post' was called on null.
Future<http.Response> login(
{#required String username, #required String password}) async {
final String url = theUrl;
final String param = "login=$username&password=$password";
final response = await client.post(url, body: param, encoding: Encoding.getByName("UTF-8");
return response;
}
Would you mind to explain to me what this means exactly?
Full error log:
E/flutter ( 7161): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 7161): NoSuchMethodError: The method 'post' was called on null.
E/flutter ( 7161): Receiver: null
E/flutter ( 7161): Tried calling: post("https://.../login", body: _LinkedHashMap len:2, encoding: Instance of 'Utf8Codec')
E/flutter ( 7161): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter ( 7161): #1 ApiProvider.login (package:flutter_mvp/resources/api_provider.dart:25:35)
E/flutter ( 7161): <asynchronous suspension>

This means that the client variable was null at the time of the call.
You will have to make sure that your client is initialized before calling login.

If you use VS code that is probably because of automatic solution. Just search in code as get http => null; and delete it. Instead of add
import 'package:http/http.dart' as http; to imports.

Related

Value type error in a map that is <String, dynamic>

I'm trying to construct a https Uri that looks like this:
static Uri accountGetFavoriteMovies(int accountId, LoginInformation loginInfo,
{int page = 1}) {
final url = '/account/$accountId/favorite/movies';
final uri = _resolveUri(
url,
query: {
'api_key': _apiKey,
'session_id': loginInfo.sessionId,
'page': page,
},
);
return uri;
}
And what _resolveUri does simply a wrapper to Uri.https constructor:
Uri _resolveUri(String path, {Map<String, dynamic>? query}) {
return Uri.https(
_authority,
_basePath + path,
query,
);
}
Theoretically this should be working fine, but when I tested it, and I got this message:
type 'int' is not a subtype of type Iterable<dynamic>
Where's the problem?
I am convinced the problem is in the query parameter because of this stack:
_resolveUri
_AccountUrls.accountGetFavoriteMovies
_AccountRequest.favoriteMoviesRequest
Although the type for queryParameters is Map<String, dynamic>, the value expected by Uri.https is actually String or Iterable<String>.
I found this comment in the source code of the Uri library on github while trying to debug your question.
A value in the map must be either a String, or an Iterable of strings,
where the latter corresponds to multiple values for the same key.
Source of the Quote
Other Comment of Importance

Getting 'String is not a subtype of type 'List<dynamic>' error when calling clientViaServiceAccount

I'm trying to add a user's first name and last name to cells A and B to the target google sheet but I keep getting the error :
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type
'String' is not a subtype of type 'List' in type cast
whenever I submit my code.
I know that the error is isolated to the clientViaServiceAccount block of code because that's the line of code that the error stack points to. I've tried changing _scopes to [SheetsApi.SpreadsheetsScope] in the parameters but that led to the same thing.
I modeled this block of code based off of the example here:
https://pub.dartlang.org/packages/googleapis#-example-tab-
and the second answer here:
Google Sheets API v4 for Flutter/Dart
if (_formKey.currentState.validate()){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ConfirmationPage()),
);
_formKey.currentState.save();
final _privateKey = { 'private JSON key' };
final _cred = new ServiceAccountCredentials.fromJson(_privateKey);
final _scopes = [SheetsApi.SpreadsheetsScope];
Map<String,String> _sheets = {
"ISMI": 'sheet key',
"NORCO": 'sheet key 2'
};
clientViaServiceAccount(_cred, _scopes).then((client){
var api = new SheetsApi(client);
ValueRange vr = new ValueRange.fromJson({"values": [_newUser.firstName,_newUser.lastName]});
api.spreadsheets.values.append(vr,_sheets[_newUser.location],'A:B').then((AppendValuesResponse r) {
client.close();
});
});
}
Any help would be much appreciated,
Thank You
The problem was that the JSON needs to be surrounded by r''''''. So I changed the _privateKey variable to this:
final _privateKey = r'''{private JSON key}''';

flutter - Dart Map in Map called on null

I have this
var _subscription = Map<String, Map<String, StreamSubscription<DocumentSnapshot>>>();
When I tried to call this
_subscription[id][userId] = Firestore.instance.collection('user').document(userId).snapshots().listen((snapshot)
console says this.
The method '[]=' was called on null
Does anyone know what am I missing?

Getting error on Nop Commerce application

I am getting error like
An exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll but was not handled in user code
Additional information: An exception was thrown while invoking the constructor 'Void .ctor()' on type 'UsaEpayService'.
Invalid URI: The URI is empty.
Getting above exception on Resolve -
Path : Libraries\Nop.Core\Infrastructure\DependencyManagement\ContainerManager.cs
Here is my code:
public virtual object Resolve(Type type, ILifetimeScope scope = null)
{
if (scope == null)
{
//no scope specified
scope = Scope();
}
return scope.Resolve(type);
}
I have cross-posted this here.

AngularDart Filter call method throws error when 'items' is null

When I load a component using 'filter', the 'call' method is being run before the 'items' parameter is being set. So I am getting repeating errors from line 200. Eventually, 'items' is sent and the 'call' method runs without error.
The null object does not have a method 'where'.
NoSuchMethodError: method not found: 'where'
Receiver: null
Arguments: [Closure: (dynamic) => dynamic]
STACKTRACE:
#0 Object._noSuchMethod (dart:core-patch/object_patch.dart:42)
#1 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#2 Filter.call (package:angular/formatter/filter.dart:200:26)
This is the first time I have implemented filter using AngularDart, and this is happening in both components I've tried. There's nothing special in my template:
<tr ng-repeat="appSession in appSessions | orderBy: orderByList | filter: filterObject">
The appSessions list is either set by an attribute or if not provided, in attach method. In this case, it is being set by the attribute.
#NgOneWay("app-sessions") List<AppSessionModel> appSessions;
#NgOneWay("mobile-user-id") int mobileUserID;
void attach() {
if (appSessions == null) {
if (mobileUserID != null) {
appSessionManager.getForUser(mobileUserID).then((sessions) {
appSessions = sessions;
});
} else if (!fromUserTable) {
appSessionManager.getOpen().then((sessions) {
appSessions = sessions;
});
}
}
}
Has anyone else experienced this? Is there a timing bug here?
Thanks,
Sam
After more debugging, I confirmed that my list was never null in the 'attach' method, and I confirmed that Filter.call was running after the 'attach' method. It turns out that the problem related to the 'orderBy' in the template.
I had this:
<tr ng-repeat="appSession in appSessions | orderBy: orderByList | filter: filterObject">
If I switch 'orderBy' and 'filter', then no more Filter.call error:
<tr ng-repeat="appSession in appSessions | filter: filterObject | orderBy: orderByList">
I debugged OrderBy.call, but it is not returning 'items' as null, so the problem seems to be somewhere between OrderBy and Filter, but I don't know enough about AngularDart to know what that might be.
You could just initialize the field with an empty list
List<AppSessionModel> appSessions = [];

Resources