How to pass the Arraylist of custom Object type from activity to Service using the Parceler library? - parceler

I have done this in activity.Here responses variable is the ArrayList of type custom object LeaseDetailResponse.
Intent intent = new Intent(TestActivity.this, AlarmService.class);
intent.putParcelableArrayListExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, (ArrayList<? extends Parcelable>) Parcels.wrap(responses));
startService(intent);
At AlarmService
Parcels.unwrap((Parcelable) intent.getParcelableArrayListExtra(LEASE_DETAIL_RESPONSE_LIST));
Shows the error
java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList
Caused by: java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList

The issue is with your cast to an ArrayList... Parceler only deals with Parcelable. You need to use putExtra() instead with no cast:
Intent intent = new Intent(TestActivity.this, AlarmService.class);
intent.putExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, Parcels.wrap(responses));
startService(intent);
And to de-serialize in your AlarmService:
Parcels.unwrap(intent.getParcelableExtra(LEASE_DETAIL_RESPONSE_LIST));

Related

Use a method on a StateNotifier Riverpod for changing a bool [duplicate]

In the context of a Flutter 2.0.5 app whose state I'd like to manage with Riverpod, I thought I can declare a StateNotifierProvider like this:
import 'package:flutter_riverpod/flutter_riverpod.dart';
final counterProvider = StateNotifierProvider<CounterStateNotifier>((ref) => CounterStateNotifier());
class CounterStateNotifier extends StateNotifier<int> {
CounterStateNotifier([int count = 0]) : super(count);
void increment() => state++;
}
But Android Studio (and later the Dart compiler as well) complains about the line where I declare the counterProvider variable:
The type 'StateNotifierProvider' is declared with 2 type parameters, but 1 type arguments were given.
Removing the <CounterStateNotifier> type parameter in StateNotifierProvider<CounterStateNotifier> removes the error. However, attempting to read the provider and call its increment method (setting () => context.read(counterProvider).increment() as the onPressed of an ElevatedButton, then pressing the button) gives the following runtime error:
'increment'
method not found
Receiver: 0
Arguments: []
Why is context.read(counterProvider) returning the int state instead of the notifier? And what is the reason behind the type parameter error mentioned in the first part of my question?
I should mention that I'm running my app on the web (with flutter run -d Chrome).
As of Riverpod 0.14.0, State is the default value exposed by StateNotifierProvider.
The syntax for declaring your StateNotifierProvider is now as follows:
final counterProvider = StateNotifierProvider<CounterStateNotifier, int>((ref) => CounterStateNotifier());
Accessing functions now requires adding .notifier (accessing the StateNotifier itself):
context.read(counterProvider.notifier).increment();
And like you've noticed, you now access the state like so:
final count = context.read(counterProvider);
More on the changes here.
You may also use dynamic to accept any type if value for the StateNotifierProvider
final modelProvider =
StateNotifierProvider.autoDispose<ModelClassName, dynamic>(
(ref) => ModelClassName());

Zend framework 3 form issue with binding entities that use return type hinting

As I'm upgrading my application to PHP 7.3, I decided to add return type hinting to my entities methods.
An example would be something like this:
class User {
private $username;
public method getUsername(): string { return $this->username}
}
I use this entity in a form (to create users) where I bind it like so:
$user = new User();
$form->bind($user);
$form->setData($data);
However I get this error which is caused by the hydrator (I'm using the DoctrineObject hydrator)
Return value of Admin\Entity\Auth\User::getUsername() must be of the type string, null returned
#0 ...\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(220): Admin\Entity\Auth\User->getUsername()
#1 ...\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(116): DoctrineModule\Stdlib\Hydrator\DoctrineObject->extractByValue(Object(Admin\Entity\Auth\User))
#2 ...\vendor\zendframework\zend-form\src\Fieldset.php(650): DoctrineModule\Stdlib\Hydrator\DoctrineObject->extract(Object(Admin\Entity\Auth\User))
...
From the snippet above it's obvious that it's calling the getUsername method on an "empty" object, which makes it return null and throw the exception.
This is not logically correct as the entity is supposed to have a username so marking it as nullable (?string) is not correct.
Is there any better solution to this? Because making every single method return type nullable seems like a hack that defeats the purpose.

DryIoc cannot resolve my boolean instance?

I try to register a class which constructor requires a few string, a ILog and a bool. All in my container is registered as a singleton (set as the default Reuse).
But whenever I try to get an instance of that class container.Resolve<AzmanAccess>(), an exception gets thrown.
Unable to resolve Boolean as parameter "accessAll" in
Company.Common.Util.Authentication.AzmanAccess:
Company.Common.Util.Authentication.IAzmanAccess
{RequiredServiceType=Company.Common.Util.Authentication.AzmanAccess}
as parameter "azmanAccess" in
Company.Common.Util.Authentication.AzmanCustomAccess:
Company.Common.Util.Authentication.IAccess as parameter "access" in
Company.Common.Util.Authentication.User:
Company.Common.Util.Authentication.IUser Where CurrentScope: null and
ResolutionScope: null and Found registrations: skipAuthz,{ID=53,
ImplType=Boolean, Reuse=SingletonReuse {Lifespan=1000}}}
System.Object,{ID=25, ImplType=Boolean, Reuse=SingletonReuse
{Lifespan=1000}}}
I register my class like this:
container.RegisterInstance(_accessAll, serviceKey: AccessAll);
container.RegisterInstance(_activeDirectoryDomain, serviceKey: ActiveDirectoryDomain);
container.RegisterInstance(_azmanConnString, serviceKey: AzmanConnString);
container.RegisterInstance(_azmanStore, serviceKey: AzmanStore);
container.Register(Made.Of(() => new AzmanAccess(
Arg.Of<bool>(AccessAll),
Arg.Of<ILog>(),
Arg.Of<string>(ActiveDirectoryDomain),
Arg.Of<string>(AzmanConnString),
Arg.Of<string>(AzmanStore)
)));
and the constructor is:
public AzmanAccess(bool accessAll, ILog logger, string activeDirectoryDomain, string azmanConnString, string azmanStore)
Where servicekeys (AccessAll, ...) are unique (tried with objects, then string).
What's going wrong?
The problem caused by the bug in DryIoc: Arg.Of does not recognize service key of non-primitive type.
Until bug is fixed the workaround is to change object service key to e.g. string, Enum type, or int.
Update:
The fix is available in DryIoc v2.4.3.

Unknown database type enum requested,Doctrine

when i want to generate Entity from data base i have this Error:
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it
how can i resolve this issue.
Thanks in advance
You could try to do something like this in the onBootstrap module of your Module.php, to tell Doctrine to treat your enum like a string
$em = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Add the following line to your bootstrap.php
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
If you really want to work with enums and don't convert them to strings, you should implement your custom type (it's really not a big deal).
See enter link description here
But also, you have to extend list of types on your platform.
So, simplest way to do that - override useless method \Doctrine\DBAL\Types\Type::getMappedDatabaseTypes with your own so like that:
class EnumType extends Type
{
const NAME = "enum";
// ... (your implemented methods)
public function getMappedDatabaseTypes(AbstractPlatform $platform)
{
return ['enum'];
}
}
Have a fun :)

Missing descriptor exception while using Eclipselink Expressionbuilder

I have an entity Person with fields String name and String designation. When I tried to query using Eclipselink ExpressionBuilder, as:
Project project=new Project();
Login login=new DatabaseLogin();
login.setUserName("root");
login.setPassword("root");
project.setLogin(login);
DatabaseSession session=project.createDatabaseSession();
ExpressionBuilder expBuilder=new ExpressionBuilder();
Expression expression=expBuilder.get("name").equalsIgnoreCase("SomeName");
Vector readAllObjects = session.readAllObjects(Person.class, expression);
On executing last statement the following exception is thrown :
Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class com.mycompany.entity.Person].
Query: ReadAllQuery(referenceClass=Person)
What might be the reason? Thanks in advance...
At last I got it. Instead of DatabaseSession, org.eclipse.persistence.sessions.server.ClientSession had to be used.
JpaEntityManager jpaEntityManager=em.unwrap(JpaEntityManager.class);
ClientSession session=jpaEntityManager.getServerSession().acquireClientSession();
ExpressionBuilder expBuilder=new ExpressionBuilder();
Expression expression=expBuilder.get("name").equalsIgnoreCase("SomeName");
Vector readAllObjects = session.readAllObjects(Person.class, expression);
em is the EntityManager.
This solved the issue.
You did not add a descriptor for Person. You need to map it to be able to query it.
Also consider using JPA.